How to Share a Draft Page in WordPress Without a Plugin

How to Share a Draft Page in WordPress Without a Plugin

WordPress has many ways to restrict who sees your content. For example, you can create a password protected post that’s hidden unless the viewer knows the secret key. Or you can create a private post that’s visible only to logged in users, and not anyone else outside. These systems of protection have their uses, but they’re limited in many ways. Password protection requires you to first publish the post, and only then allow someone to access it. This means that search engines like Google will pick up your URL and be blocked. And private posts don’t allow you to share them with anonymous users. There’s no in-built way in WordPress to share a draft page or post with users who aren’t logged into your site.

You’d like to avoid publishing a post until it’s ready since you don’t want users to see something with errors. The same goes for search engines. It’s not a good idea to have an outdated version of your page in the search cache.

There are many use cases for this kind of situation. You might want to give a sneak peak of your post to a friend without having them sign up on your blog. Or perhaps you’re writing a sponsored article and wish you show the final work to your client without publishing the post. Or maybe you need to get approval for the post from your wife before you publish (Ok, that’s just me!). Whatever the reason, there is a real need in WordPress to share draft posts by URL before actively publishing them.

In this article, I’ll show you a quick way to accomplish this without a plugin. All you need to do is append a “key” to the URL, and anyone can view your draft posts if they know what it is.

Share a Draft Page in WordPress via URL

Open up your functions.php file in WordPress, or whatever location you use for custom PHP code, and paste in the following:

add_filter( 'posts_results', 'set_query_to_draft', null, 2 );
function set_query_to_draft( $posts, &$query ) {

    if ( sizeof( $posts ) != 1 )
        return $posts;

    $post_status_obj = get_post_status_object(get_post_status( $posts[0]));

    if ( !$post_status_obj->name == 'draft' )
        return $posts;

    if ( $_GET['key'] != 'private_preview' )
        return $posts;

    $query->_draft_post = $posts;

    add_filter( 'the_posts', 'show_draft_post', null, 2 );
}

function show_draft_post( $posts, &$query ) {
    remove_filter( 'the_posts', 'show_draft_post', null, 2 );
    return $query->_draft_post;
}

Testing it Out

If you don’t know how to add code snippets to WordPress, check out my earlier tutorial on the same. With this code added, you can add the following URL parameter to the preview URL, to let others have access:

&key=private_preview

So for example, say the URL of a draft post is:

https://www.webhostingshed.com/?p=4565&preview=true

Then if you want to share the draft with a third party without making them sign up on your site, change it to:

https://www.webhostingshed.com/?p=4565&preview=true&key=private_preview

You can see in the screenshot below, that the draft is visible in Chrome incognito mode:

Share draft page in WordPress

You can change the value of the key variable in the “bold” piece of code above, so that it remains a secret between you and whoever else you’re sharing it with. It’s a bit risky because anyone with the key value can preview all your draft posts if they know the post number! You can work around this by de-activating the code snippet after your collaboration work is done, or change the key value immediately when the need ends. Either way, it’s a security loophole that you should be aware of.

This is a great way to share draft pages in WordPress. But there’s also a nice plugin called Public Post Preview which provides some additional features such as post expiry with the WordPress Nonce functionality, so that the posts are not visible in perpetuity. However, if the idea of installing a separate plugin doesn’t appeal to you, then the above piece of code is a great solution.

One Reply to “How to Share a Draft Page in WordPress Without a Plugin”!

Leave a Reply

This Website is Hosted by

Powered by RamNode

Disclosure: We receive a compensation from some of the companies whose products or services are presented on our website.