Share a Draft Post in WordPress Without a Plugin

Wordpress, Website Design

General Graphic: Code Photo by Irvan Smith

WordPress is great for locking away your content from people who shouldn’t see it. However, there are times when you need a second set of eyes on a page or a post before you make it available to the public.

By default, WordPress already gives you options to create drafts of your posts, password-protect your posts, or even set your posts to private mode. In all of these cases, if you want other people to be able to view the content, there is going to be some type of password involved—either they will have to log into your website or enter a password on the page itself.

But what if you just want to share a URL without requiring the viewer to login? Well, there are a few ways to do that too! The most popular way is to install yet another plugin to handle this functionality for you. Public Post Preview and WP-DraftsForFriends are two popular solutions in the plugin world.

I personally don’t like to install new plugins when I can avoid it. So here is how you can share a draft post by URL in WordPress without a plugin. It’s as simple as adding a few extra lines to your functions.php file!

How to Share a Draft Post or Page in WordPress Without a Plugin


Step 1:
Make a backup of your theme’s functions.php file—just in case.

Step 2:
Open your functions.php in a code or text editor, scroll to the bottom, and paste the following code:

//* Enable Private Previews
//* ---------------------------------------------------------------------------

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

    // Change these values (optional)
    $unlock_key = 'status';
    $unlock_values = array( 'proofreader' );

    // Don't modify below.
    if ( sizeof( $posts ) != 1 ) { return $posts; }
    $post_status_obj = get_post_status_object( get_post_status( $posts[0] ) );
		$passed_check = false;
    if ( !$post_status_obj->name == 'draft' )
        return $posts;
    foreach ( $unlock_values as $unlock_value ) {
        if ( $_GET[$unlock_key] === $unlock_value )
            $passed_check = true;
    }
    if ( $passed_check !== true )
        return $posts;
    // if ( $_GET[$unlock_key] != $unlock_value )
    $query->_draft_post = $posts;
    add_filter( 'the_posts', 'display_draft_post', null, 2 );
}

function display_draft_post( $posts, $query ) {
    remove_filter( 'the_posts', 'display_draft_post', null, 2 );
    return $query->_draft_post;
}
//* ---------------------------------------------------------------------------

Step 3:
Upload your modified functions.php and test out your work.


How to Test Your Shareable URLs

Open a new private browsing window (this will make WordPress think you a guest visitor) and go to the URL for your draft post. (It should come up as a 404 / Not Found page at this point.) Now add ‘&status=proofreader’ to the end of your URL and try it again.

For example, if your URL is:
https://yourdomain.com/?p=123&preview=true

Then your modified (and shareable!) URL should look like:
https://yourdomain.com/?p=123&preview=true&status=proofreader

Yay! Now for any draft that you want to share without having to make your reader login, all you need to do is add the status code to the end of the URL.


Change or Create Multiple Shareable URLs

If you’re looking to lock down your security, consider occasionally changing the values of the two variables in Step 2 called $unlock_key and within the comma-separated array for $unlock_values. By changing these values, you can have your super-secret URLS accessible by any combination of words that you like! You can even add different case-sensitive URLs by adding more comma-separated values.

Example:
$unlock_key = 'secret';
$unlock_values = array( 'proofreader', 'Carly', 'myhusband' );

Changing those values will make your draft post viewable by any of the following URLs:
https://yourdomain.com/?p=123&preview=true&secret=proofreader
https://yourdomain.com/?p=123&preview=true&secret=Carly
https://yourdomain.com/?p=123&preview=true&secret=myhusband

But remember that it’s case-sensitive so the URL https://www.yourdomain.com/?p=123&preview=true&secret=carly with a lowercase ‘c’ will not work.


That’s it! If this article has been helpful to you, please share it with others!

You may also like

shares