Drafts Action Directory

Post to WordPress w/ preview

Last update 4 days ago - Unlisted

Post a standard post to a WordPress site after previewing how it will look on your site. Set the post title in Title Case, and create a slug with the same name. Allows for the addition of a category and a comma-separated list of tags via text input. Default post status is draft. Added support for the MultiMarkdown improvements in the Drafts 5.5 release.

Note: you’ll need to change the URL in the last step to your admin page URL of WordPress as well as the CSS in the HTML preview step.

Install

Steps

  • script

    /* ------Functions------ */
    
    // Title Case Function
    
    String.prototype.toTitleCase = function(){
      var smallWords = /^(a|an|and|as|at|but|by|en|for|if|in|nor|of|on|or|per|the|to|vs?\.?|via)$/i;
     
      return this.replace(/[A-Za-z0-9\u00C0-\u00FF]+[^\s-]*/g, function(match, index, title){
        if (index > 0 && index + match.length !== title.length &&
          match.search(smallWords) > -1 && title.charAt(index - 2) !== ":" &&
          (title.charAt(index + match.length) !== '-' || title.charAt(index - 1) === '-') &&
          title.charAt(index - 1).search(/[^\s-]/) < 0) {
          return match.toLowerCase();
        }
        if (match.substr(1).search(/[A-Z]|\../) > -1) {
          return match;
        }
        return match.charAt(0).toUpperCase() + match.substr(1);
      });
    };
    
    // Hyphenate Function
    function hyphenate(s) {
      var f       = ' '
        , r       = '-'
        , re      = new RegExp(f, 'g')
        , matches = s.match(re);
      if (matches) {
        return s.replace(re,r);
      }
      else {
    //    app.displayWarningMessage("No spaces in selection")
        return s
       
      }  
    }
  • script

    // Convert MMD text w/ Critic Markup to standard Markdown
    
    /* --- Post --- */
    let previewText = draft.content;
    let previewMMD = MultiMarkdown.create();
    previewMMD.criticMarkup = true;
    previewMMD.criticMarkupAccept = true;
    let post = previewMMD.render(previewText)
    draft.setTemplateTag("post",post);
  • htmlpreview

    <html>
    <head>
    <title>nahumck.me Preview</title>
    <style type="text/css">
    /*
    Ulysses WordPress Preview: nahumck.me (based on WP Twenty Sixteen)
    */
    
    * {
    -webkit-text-size-adjust:	none;
    -webkit-font-smoothing: antialiased;
    text-rendering: optimizelegibility !important;
    }
    
    body {
    color: #f2f2f2;
    font-family: IBMPlexMono, sans-serif;
    font-size: 16px;
    font-size: 1rem;
    line-height: 1.75;
    background-color: #424242;
    width: 75%;
    margin-left: auto;
    margin-right: auto;
    padding-bottom: 25px;
    margin-bottom: 25px;
    }
    
    @media screen and (max-width: 668px) {
    body {
    margin-right: 7.6923%;
    margin-left: 7.6923%;
    width: auto;
    }
    }
    
    p {
    margin: 0 0 1.75em;
    }
    
    h1, h2, h3, h4, h5, h6 {
    clear: both;
    font-weight: 700;
    margin: 0;
    text-rendering: optimizeLegibility;
    }
    
    h1 {
    font-weight: 900;
    font-size: 26px;
    font-size: 1.75rem;
    line-height: 1.25;
    margin-top: 2em;
    margin-bottom: 1em;
    }
    
    h1.entry-title {
    font-family: IBMPlexMono, sans-serif;
    font-size: 28px;
    font-size: 1.75rem;
    font-weight: 700;
    line-height: 1.25;
    margin-bottom: 1em;
    }
    
    h1.entry-title a {
    box-shadow: none;
    text-decoration: none;	
    color: #f2f2f2;
    }
    
    h1.entry-title a:hover {
    box-shadow: 0 1px 0 0 currentColor;
    }
    
    h2 {
    font-size: 23px;
    font-size: 1.4375rem;
    line-height: 1.2173913043;
    margin-top: 2.4347826087em;
    margin-bottom: 1.2173913043em;
    }
    
    h3 {
    font-size: 19px;
    font-size: 1.1875rem;
    line-height: 1.1052631579;
    margin-top: 2.9473684211em;
    margin-bottom: 1.4736842105em;
    }
    
    h4, h5, h6 {
    font-size: 16px;
    font-size: 1rem;
    line-height: 1.3125;
    margin-top: 3.5em;
    margin-bottom: 1.75em;
    }
    
    h4 {
    letter-spacing: 0.140625em;
    text-transform: uppercase;
    }
    
    
    h6 {
    font-style: italic;
    }
    
    hr {
    background-color: #f2f2f2;
    border: 0;
    height: 1px;
    margin: 0 0 1.75em;
    }
    
    em {
    font-style: italic;
    }
    
    strong {
    font-style: bold;
    }
    
    small {
    font-size: 80%;
    }
    
    sub, sup {
    font-size: 75%;
    line-height: 0;
    position: relative;
    vertical-align: baseline;
    }
    
    sub {
    bottom: -0.25em;
    }
    
    sup {
    top: -0.5em;
    }
    
    a {
    color: #f7ea00;
    text-decoration: none;
    box-shadow: 0 1px 0 0 currentColor;
    background-color: transparent;
    }
    
    ul, ol {
    margin: 0 0 1.75em 1.25em;
    padding: 0;
    }
    
    ol {
    list-style: decimal;
    margin-left: 1.5em;
    }
    
    ul {
    list-style: disc;
    }
    
    blockquote {
    quotes: none;
    border: 0 solid #c9c9c9;
    border-left-width: 4px;
    color: #f2f2f2;
    font-size: 16px;
    font-size: 1.1875rem;
    font-style: italic;
    line-height: 1.4736842105;
    margin: 0 0 1.4736842105em;
    padding: 0 0 0 1.263157895em;
    }
    
    code, kbd, tt, var, samp, pre {
    font-family: IBMPlexMono, san-serif;
    font-size: 1em;
    }
    
    pre {
    border: 1px solid #d1d1d1;
    font-size: 16px;
    font-size: 1rem;
    line-height: 1.3125;
    margin: 0 0 1.75em;
    max-width: 100%;
    overflow: auto;
    padding: 1.75em;
    white-space: pre;
    white-space: pre-wrap;
    word-wrap: break-word;
    }
    
    /*code block color changes*/
    
    code,pre {
    color:#f2f2f2;
    background-color:#424242;
    background-style:fill;
    border:1px;
    border-style:solid;
    border-color:#f2f2f2;
    border-radius:5px;
    padding:4px;
    }
    
    figure {
    margin: 0;
    margin-bottom: 1.75em;
    max-width: 100%;
    }
    
    img {
    height: auto;
    /* Make sure images are scaled correctly. */
    max-width: 100%;
    /* Adhere to container width. */
    vertical-align: middle;
    border: 0;
    }
    
    figcaption {
    color: #686868;
    font-size: 13px;
    font-size: 0.8125rem;
    font-style: italic;
    line-height: 1.6153846154;
    padding-top: 0.5384615385em;
    }
    
    
    table, th, td {
    border: 1px solid #d1d1d1;
    }
    
    table {
    border-collapse: separate;
    border-spacing: 0;
    border-width: 1px 0 0 1px;
    margin: 0 0 1.75em;
    table-layout: fixed;
    /* Prevents HTML tables from becoming too wide */
    width: 100%;
    }
    
    
    th, td {
    padding: 0.4375em;
    }
    
    caption, th, td {
    font-weight: normal;
    text-align: left;
    }
    
    th {
    border-width: 0 1px 1px 0;
    font-weight: 700;
    }
    
    td {
    border-width: 0 1px 1px 0;
    }
    
    div.entry-summary {
    color: #686868;
    font-size: 19px;
    line-height: 28px;
    
    margin-top: -21px;
    margin-left: 0px;
    margin-bottom:	42px;
    margin-right: 0px;
    
    word-wrap: break-word;
    }
    
    div.post-thumbnail {
    margin-top: 0px;	
    margin-left: 0px;
    margin-bottom:	42px;
    margin-right: 0px;
    }
    
    /*link changes*/
    
    .entry-content a,
    .entry-summary a,
    .taxonomy-description a,
    .logged-in-as a,
    .comment-content a,
    .pingback .comment-body > a,
    .textwidget a,
    .entry-footer a,
    .entry-footer a:hover,
    .site-info a:hover {
    box-shadow: none;
    }
    
    .entry-content a,
    .entry-footer a {
    text-decoration:underline;
    }
    
    /*change auto-hyphens for post titles*/
    
    .post-navigation .post-title, 
    .entry-title, 
    .comments-title {
      -webkit-hyphens: manual !important;
      -moz-hyphens: manual !important;
      -ms-hyphens: manual !important;
      hyphens: manual !important;
    }
    
    /*border change for site*/
    
    @media screen and (min-width: 44.375em) {
      body:not(.custom-background-image):before,
      body:not(.custom-background-image):after {
        height: 2.5px;
      }
      .site {
        margin: 2.5px;
      }
    }
    
    /* FONTS */
    @font-face {
    font-family: IBMPlexMono;
    font-style: normal;
    font-weight: normal;
    src: url("https://nahumck.me/wp-content/fonts/IBMPlexMono/IBMPlexMono-Regular.otf");
    }
    
    @font-face {
    font-family: IBMPlexMono-Bold;
    font-style: normal;
    font-weight: normal;
    src: url("https://nahumck.me/wp-content/fonts/IBMPlexMono/IBMPlexMono-Bold.otf");
    }
    
    @font-face {
    font-family: IBMPlexMono-Italic;
    font-style: normal;
    font-weight: normal;
    src: url("https://nahumck.me/wp-content/fonts/IBMPlexMono/IBMPlexMono-Italic.otf");
    }
    
    @font-face {
    font-family: IBMPlexMono-BoldItalic;
    font-style: normal;
    font-weight: normal;
    src: url("https://nahumck.me/wp-content/fonts/IBMPlexMono/IBMPlexMono-BoldItalic.otf");
    }
    </style>
    </head>
    <body>
        <div>
    	%%[[post]]%%
    </div>
    </body></html>
  • script

    /* --- Title --- */
    var t = draft.title;
    // need to search for matches within brackets somehow.
    t = t.replace(/# /g, "");
    var titleRegex = new RegExp(/\[(.*?)\]/);
    var check = t.match(titleRegex);
    if (check) {
    	t = check[1]
    }
    var postTitle = t.toTitleCase();
    draft.setTemplateTag("postTitle",postTitle)
    
    /* --- Slug --- */
    var s = t.toLowerCase();
    s = s.replace(/[^a-zA-Z0-9 ]/g, "-")
    var slug = hyphenate(s);
    slug = slug.replace(/---?/g, "-");
    draft.setTemplateTag("slug",slug);
    
    /* --- Content --- */
    let text = draft.processTemplate("[[body]]").trim();
    let mmd = MultiMarkdown.create();
    mmd.format = "mmd"
    mmd.criticMarkup = true;
    mmd.criticMarkupAccept = true;
    let content = mmd.render(text)
    draft.setTemplateTag("content",content);
  • script

    /* --- Categories & Tags --- */
    app.displayInfoMessage("Fetching categories & tags…")
    // create credentials for site
    let cred = Credential.createWithHostUsernamePassword("WordPress", "WordPress credentials. Include full URL (with http://) of the home page of your WordPress site in the host field.");
    cred.authorize();
    
    // set website for URL step
    let website = cred.getValue("host");
    draft.setTemplateTag("website",website);
    
    // create WordPress object and make request
    let wp = WordPress.create(cred.getValue("host"), 1, cred.getValue("username"), cred.getValue("password"));
    
    // Get Categories
    var cats = wp.getCategories();
    var categoryList = [];
    if (cats) {
    	for (let cat of cats) {
    		categoryList.push(cat.name)
    	}
    }
    
    // Get Tags
    var tags = wp.getTags();
    var tagList = [];
    if (tags) {
    	for (let tag of tags) {
    		tagList.push(tag.name)
    	}
    }
    
    var p = Prompt.create();
    p.title = "Categories";
    p.addSelect("categories","Categories", categoryList,[""],true);
    p.addTextField("newCat","New Category","");
    p.addButton("Next: Select Tags")
    var con = p.show();
    if (!con) {
    	context.cancel
    }
    var newCat = p.fieldValues["newCat"];
    var categories = p.fieldValues["categories"]
    categories = categories.slice(1)
    if (newCat.length > 0) {
    	categories.push(newCat)
    }
    draft.setTemplateTag("categories",categories)
    
    var p = Prompt.create();
    p.title = "Tags";
    p.addSelect("tags","Tags",tagList,[""],true);
    p.addTextField("newTag","New Tag","");
    p.addButton("Post Draft to WordPress")
    var con = p.show();
    if (!con) {
    	context.cancel
    }
    var newTag = p.fieldValues["newTag"];
    var tags = p.fieldValues["tags"];
    tags = tags.slice(1)
    if (newTag.length > 0) {
    	tags.push(newTag)
    }
    draft.setTemplateTag("tags",tags);
    app.displayInfoMessage("Posting to WordPress…")
  • wordPress

    titleTemplate
    [[postTitle]]
    template
    [[content]]
    postStatus
    draft
    format
    standard
    categoryTemplate
    [[categories]]
    tagTemplate
    [[tags]]
    slugTemplate
    [[slug]]
    excerptTemplate
  • file

    fileNameTemplate
    [[postTitle]].md
    folderTemplate
    template
    [[draft]]
    local
    false
    writeType
    replace
  • dropbox (disabled)

    fileNameTemplate
    [[postTitle]].md
    folderTemplate
    /Posts/
    template
    [[draft]]
    writeType
    replace
  • googleDrive (disabled)

    fileNameTemplate
    [[postTitle]].md
    folderTemplate
    /Posts/
    template
    [[draft]]
    format
    markdown
    writeType
    replace
    outputFormat
    text
  • script

    app.displaySuccessMessage("Posted to WP & File Saved to Cloud");
  • url

    template
    [[website]]/wp-admin/
    useSafari
    true
    encodeTags
    false

Options

  • After Success Nothing
    Notification Error
    Log Level Error
Actions available in the Action Directory are uploaded by community members. Use appropriate caution reviewing downloaded actions before use.