Search

How to Share a Post Draft in WordPress Before You Publish It (Without Plugin)

WordPress is a popular content management system (CMS) that allows users to create and manage websites and blogs. One of the features that makes WordPress so popular is its ease of use. However, there are some things that WordPress doesn’t do out of the box, such as sharing a post draft with someone.

If you need to share a post draft with someone, there are a few ways to do it. One way is to use a plugin. Some plugins allow you to share post drafts, but they can add complexity to your WordPress site.

This article will show you how to share a post draft in WordPress without a plugin. We will also provide you with a code snippet that you can use to share your post drafts.

Sharing a Post Draft in WordPress Before Publishing (without Plugin)

Sharing a post draft in WordPress without the help of any third-party plugins is very easy. The only thing that you need to do is add a code snippet to the functions.php file of the WordPress theme you use.

1. Add the functions.php File

On your WordPress dashboard, go to Appearance -> Theme File Editor (Theme File Editor is located under the Tools menu if you use a block theme). Once you enter the Theme File Editor page, select the functions.php file. Afterward, paste the PHP snippet below at the end of the functions file content. Next, update the theme file by clicking the Update File button.

add_action('pre_get_posts', 'allow_draft_preview');
 
function allow_draft_preview($query) {
    if (isset($_GET['key']) && $_GET['key'] == 'guest') {
        if ($query->is_main_query()) {a
            $query->set('post_status', array('publish', 'draft'));
        }
    }
}

The Code Explanation:

The PHP code snippet above is a WordPress action hook and function allowing your guest/s to preview your draft posts.

  • Action hook: pre_get_posts

This action hook is triggered before the main query retrieves posts from the database. It allows you to modify the query parameters before the query is run.

  • Function: allow_draft_preview

This function checks if the key GET parameter is set to guest. If it is, the function checks if the main query is being run. If so, the function sets the post_status query parameter to include published and draft posts.

So, when the guest visits your WordPress site with the key=guest GET parameter, they can preview draft posts, even if they are not logged in.

2. Preview Your Post Draft

Please create a new post or select the existing one and then preview it by clicking the Preview button.

To share your post-draft page, add “&key=guest” to the preview URL.

Your URL will look something like this:

https://yoursite.com/?p=123&key=guest

To see how it works, log out from your WordPress site and enter the URL in a new tab or window. You can also enter the URL in a new incognito window.

The Bottom Line

This article shows how easily you can share a post draft with someone in WordPress without the help of any third-party plugins. A little note: you should only use the code snippet if you need to allow guests to preview draft posts. For security reasons, it is generally best to only allow logged-in users to preview draft posts.

This page may contain affiliate links, which help support our project. Read our affiliate disclosure.
Akbar Padma

Akbar Padma

Akbar is a WordPress expert and a writer staff at WPPagebuilders. He mainly writes about Gutenberg, Elementor, Divi, and other WordPress page builders.
Want to save yearly expense up to $219? why not?

Leave a Comment