Action

Post to WordPress w/ preview

Posted by @nahumck, Last update about 1 month ago

UPDATES

about 1 month ago

Made listed.

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.

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
       
      }  
    }
  • htmlpreview

    <!DOCTYPE html>
    <html lang="en-US">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="profile" href="http://gmpg.org/xfn/11">
        <title>[[safe_title]] &#8211; nahumck.me</title>
        <script type="text/javascript">
        window._wpemojiSettings = {
            "baseUrl": "https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/72x72\/",
            "ext": ".png",
            "svgUrl": "https:\/\/s.w.org\/images\/core\/emoji\/12.0.0-1\/svg\/",
            "svgExt": ".svg",
            "source": {
                "concatemoji": "https:\/\/nahumck.me\/wp-includes\/js\/wp-emoji-release.min.js?ver=5.4.2"
            }
        };
        /*! This file is auto-generated */
        !function(e, a, t) {
            var r,
                n,
                o,
                i,
                p = a.createElement("canvas"),
                s = p.getContext && p.getContext("2d");
            function c(e, t) {
                var a = String.fromCharCode;
                s.clearRect(0, 0, p.width, p.height), s.fillText(a.apply(this, e), 0, 0);
                var r = p.toDataURL();
                return s.clearRect(0, 0, p.width, p.height), s.fillText(a.apply(this, t), 0, 0), r === p.toDataURL()
            }
            function l(e) {
                if (!s || !s.fillText)
                    return !1;
                switch (s.textBaseline = "top", s.font = "600 32px Arial", e) {
                case "flag":
                    return !c([127987, 65039, 8205, 9895, 65039], [127987, 65039, 8203, 9895, 65039]) && (!c([55356, 56826, 55356, 56819], [55356, 56826, 8203, 55356, 56819]) && !c([55356, 57332, 56128, 56423, 56128, 56418, 56128, 56421, 56128, 56430, 56128, 56423, 56128, 56447], [55356, 57332, 8203, 56128, 56423, 8203, 56128, 56418, 8203, 56128, 56421, 8203, 56128, 56430, 8203, 56128, 56423, 8203, 56128, 56447]));
                case "emoji":
                    return !c([55357, 56424, 55356, 57342, 8205, 55358, 56605, 8205, 55357, 56424, 55356, 57340], [55357, 56424, 55356, 57342, 8203, 55358, 56605, 8203, 55357, 56424, 55356, 57340])
                }
                return !1
            }
            function d(e) {
                var t = a.createElement("script");
                t.src = e, t.defer = t.type = "text/javascript", a.getElementsByTagName("head")[0].appendChild(t)
            }
            for (i = Array("flag", "emoji"), t.supports = {
                everything: !0,
                everythingExceptFlag: !0
            }, o = 0; o < i.length; o++)
                t.supports[i[o]] = l(i[o]), t.supports.everything = t.supports.everything && t.supports[i[o]], "flag" !== i[o] && (t.supports.everythingExceptFlag = t.supports.everythingExceptFlag && t.supports[i[o]]);
            t.supports.everythingExceptFlag = t.supports.everythingExceptFlag && !t.supports.flag, t.DOMReady = !1, t.readyCallback = function() {
                t.DOMReady = !0
            }, t.supports.everything || (n = function() {
                t.readyCallback()
            }, a.addEventListener ? (a.addEventListener("DOMContentLoaded", n, !1), e.addEventListener("load", n, !1)) : (e.attachEvent("onload", n), a.attachEvent("onreadystatechange", function() {
                "complete" === a.readyState && t.readyCallback()
            })), (r = t.source || {}).concatemoji ? d(r.concatemoji) : r.wpemoji && r.twemoji && (d(r.twemoji), d(r.wpemoji)))
        }(window, document, window._wpemojiSettings);
        </script>
        <style type="text/css">
        img.wp-smiley, img.emoji {
            display: inline !important;
            border: none !important;
            box-shadow: none !important;
            height: 1em !important;
            width: 1em !important;
            margin: 0 .07em !important;
            vertical-align: -0.1em !important;
            background: none !important;
            padding: 0 !important;
        }
        </style>
        <link rel='stylesheet' id='wp-block-library-css' href='https://nahumck.me/wp-includes/css/dist/block-library/style.min.css?ver=5.4.2' type='text/css' media='all'/>
        <style id='wp-block-library-inline-css' type='text/css'>
        .has-text-align-justify {
            text-align: justify;
        }
        </style>
        <link rel='stylesheet' id='bigfootjs-css' href='https://nahumck.me/wp-content/themes/independent-publisher-2-wpcom-child/css/bigfoot-default.css?ver=5.4.2' type='text/css' media='all'/>
        <link rel='stylesheet' id='nahumck_fonts-css' href='https://nahumck.me/wp-content/themes/independent-publisher-2-wpcom-child/css/sometype-mono.css?ver=5.4.2' type='text/css' media='all'/>
        <link rel='stylesheet' id='hljs-css' href='https://nahumck.me/wp-content/themes/independent-publisher-2-wpcom-child/css/hljs.css?ver=5.4.2' type='text/css' media='all'/>
        <link rel='stylesheet' id='hljs-light-css' href='https://nahumck.me/wp-content/themes/independent-publisher-2-wpcom-child/css/hljs-light.css?ver=5.4.2' type='text/css' media='all'/>
        <link rel='stylesheet' id='hljs-dark-css' href='https://nahumck.me/wp-content/themes/independent-publisher-2-wpcom-child/css/hljs-dark.css?ver=5.4.2' type='text/css' media='all'/>
        <link rel='stylesheet' id='genericons-css' href='https://nahumck.me/wp-content/plugins/jetpack/_inc/genericons/genericons/genericons.css?ver=3.1' type='text/css' media='all'/>
        <link rel='stylesheet' id='independent-publisher-2-style-css' href='https://nahumck.me/wp-content/themes/independent-publisher-2-wpcom-child/style.css?ver=5.4.2' type='text/css' media='all'/>
        <style id='independent-publisher-2-style-inline-css' type='text/css'>
        .byline, .comments-link {
            clip: rect(1px, 1px, 1px, 1px);
            height: 1px;
            position: absolute;
            overflow: hidden;
            width: 1px;
        }
        </style>
        <link rel='stylesheet' id='independent-publisher-2-block-style-css' href='https://nahumck.me/wp-content/themes/independent-publisher-2-wpcom/css/blocks.css?ver=1.0' type='text/css' media='all'/>
        <link rel='stylesheet' id='jetpack_css-css' href='https://nahumck.me/wp-content/plugins/jetpack/css/jetpack.css?ver=8.6.1' type='text/css' media='all'/>
        <script type='text/javascript' src='https://nahumck.me/wp-includes/js/jquery/jquery.js?ver=1.12.4-wp'></script>
        <script type='text/javascript' src='https://nahumck.me/wp-includes/js/jquery/jquery-migrate.min.js?ver=1.4.1'></script>
        <script type='text/javascript'>
        /* <![CDATA[ */
        var related_posts_js_options = {
            "post_heading": "h4"
        };
        /* ]]> */
        </script>
        <script type='text/javascript' src='https://nahumck.me/wp-content/themes/independent-publisher-2-wpcom-child/js/bigfoot.min.js?ver=5.4.2'></script>
        <script type='text/javascript' src='https://nahumck.me/wp-content/themes/independent-publisher-2-wpcom-child/js/highlight.min.js?ver=5.4.2'></script>
        <script type='text/javascript' src='https://nahumck.me/wp-content/themes/independent-publisher-2-wpcom-child/js/highlightjs-line-numbers.min.js?ver=5.4.2'></script>
        <script type='text/javascript' src='https://nahumck.me/wp-content/themes/independent-publisher-2-wpcom-child/js/bigfoot_function.js?ver=5.4.2'></script>
        <script type='text/javascript' src='https://nahumck.me/wp-content/themes/independent-publisher-2-wpcom-child/js/highlightjs_function.js?ver=5.4.2'></script>
    
        <style type='text/css'>
        img#wpstats {
            display: none
        }
        </style>
    
        <link rel="icon" href="https://i1.wp.com/nahumck.me/wp-content/uploads/2020/06/cropped-nahumck-n-logo-favicon-1.png?fit=32%2C32&#038;ssl=1" sizes="32x32"/>
        <link rel="icon" href="https://i1.wp.com/nahumck.me/wp-content/uploads/2020/06/cropped-nahumck-n-logo-favicon-1.png?fit=192%2C192&#038;ssl=1" sizes="192x192"/>
        <link rel="apple-touch-icon" href="https://i1.wp.com/nahumck.me/wp-content/uploads/2020/06/cropped-nahumck-n-logo-favicon-1.png?fit=180%2C180&#038;ssl=1"/>
        <meta name="msapplication-TileImage" content="https://i1.wp.com/nahumck.me/wp-content/uploads/2020/06/cropped-nahumck-n-logo-favicon-1.png?fit=270%2C270&#038;ssl=1"/>
    		<style type="text/css">
    			.content-wrapper {
    				margin: 1.5em auto 0;
    			}
    		</style>
    </head>
    
    <body class="post-template-default single single-post postid-1866 single-format-standard wp-embed-responsive author-hidden comment-hidden">
    
        <div id="page" class="hfeed site">
            <div id="hero-header" class="site-hero-section">
                <header id="masthead" class="site-header" role="banner">
                    <div class="inner">
                        <div class="site-branding">
                            <a class="site-logo-link" href="https://nahumck.me/">
                                <img alt="" class="site-logo-image no-grav" width="80" height="80" src="https://nahumck.me/wp-content/themes/independent-publisher-2-wpcom-child/images/nahumck-n.png"/>
                            </a>
                            <!-- .site-logo-link -->
                            <p class="site-title">
                                nahumck
                                <span>.</span>
                                me
                            </p>
                        </div>
                        <!-- .site-branding -->
    
                    </div>
                    <!-- .inner -->
                </header>
                <!-- #masthead -->
            </div>
    
            <div id="content-wrapper" class="content-wrapper">
                <div id="content" class="site-content">
    
                    <div id="primary" class="content-area">
                        <main id="main" class="site-main" role="main">
                                <header class="entry-header">
                                    <h1 class="entry-title">[[safe_title]]</h1>
                                </header>
                                <!-- .entry-header -->
                                <div class="entry-meta">
                                <!-- .entry-meta -->
    
                                <div class="entry-content">
    																%%[[body]]%%
    
                                </div>
                                <!-- .entry-content -->
                                <!-- .entry-footer -->
                            <!-- #post-## -->
                        </main>
                        <!-- #main -->
                    </div>
                    <!-- #primary -->
    
                </div>
                <!-- #content -->
    
                <footer id="colophon" class="site-footer" role="contentinfo">
                    <div class="footer-widgets clear">
                        <div class="widget-areas">
                            <!-- .widget-area -->
    
                        </div>
                        <!-- .widget-areas -->
                    </div>
                    <!-- .footer-widgets -->
                    <div class="site-info">
                    				&copy; 2015 - 2020 nahumck.me
                    			</div>
                    <!-- .site-info -->
                </footer>
                <!-- #colophon -->
            </div>
            <!-- #content-wrapper -->
        </div>
        <!-- #page -->
    
        <script type='text/javascript' src='https://nahumck.me/wp-content/themes/independent-publisher-2-wpcom/js/navigation.js?ver=20170317'></script>
        <script type='text/javascript' src='https://nahumck.me/wp-content/themes/independent-publisher-2-wpcom/js/independent-publisher-2.js?ver=20170406'></script>
        <script type='text/javascript' src='https://nahumck.me/wp-content/themes/independent-publisher-2-wpcom/js/skip-link-focus-fix.js?ver=20170315'></script>
    </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 --- */
    /* --- Remove Critic Markup Elements --- */
    // {--deletion--} 
    // {~~sub~>substitution~~} 
    // {++addition++}
    // {==highlight==}
    // {>>comment<<}
    let content = draft.processTemplate("[[body]]").trim();
    content = content.replace(/\{>>.*<<\}|\{--.*--\}|\{==|==\}|\{\+\+|\+\+\}|\{~~.*~>|~~\}/gi, "");
    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
  • script

    // Overwrite file in the Post Drafts folder, then move file to Posts folder
    let fm = FileManager.createCloud();
    fm.writeString("/Draft Posts/"+postTitle+".md", content);
    fm.moveItem("/Draft Posts/"+postTitle+".md", "/Posts/"+postTitle+".md");
  • script

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

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

Options

  • After Success Ask
    Notification Error
    Log Level Error
Items available in the Drafts Directory are uploaded by community members. Use appropriate caution reviewing downloaded items before use.