I'm trying to troubleshoot page previews in WordPress. I can't entirely figure out how they work. But I do know:

  1. The preview ID and preview nonce uniquely identify the preview and indicate where to pull the preview data from.

  2. The preview data is not pulled from the database since it doesn't use the stored data but data inputted into page edit fields.

  3. The preview data is not stored in $_GET because the GET parameters don't include all the data.

So is the preview data pulled from $_SESSION or some other part of the database? How does WordPress get from the form fields in the editor to the actual preview page?

  • Of note, WordPress Core doesn't make use of PHP Sessions, and a lot of managed or large scale hosts have unpredictable behaviour when PHP sessions are used. Another possibly useful tidbit of data for an answer is that what you see in a draft isn't necessarily backed by a post even if it's derived from one, it has no direct DB representation, only indirect, which is an issue for those wanting to implement it in the REST API – Tom J Nowell Nov 22 '16 at 2:51
  • 3
    Possible duplicate of Post preview mechanism architecture – Emil Nov 24 '16 at 11:46

Yes, this one is a duplicate of this post. I will provide little different examination mechanism.

You use wp cli export before and after you preview the minimal asdf post.

wp export

You don't save anything, you just preview.

Make sure you have super big autosave interval. define( 'AUTOSAVE_INTERVAL', 1000000 ); The default interval is 60 seconds.

After that, you use the diff command tool to check the two xml files. wp export will return XML files.

diff *.xml

I will get the idea this is a post (state draft) without the title with the content of asdf.

> <item>
>   <title/>
>   <link>http://test100.com/?p=1705</link>
>   <pubDate>Mon, 30 Nov -0001 00:00:00 +0000</pubDate>
>   <dc:creator>admin</dc:creator>
>   <guid isPermaLink="false">http://test100.com/?p=1705</guid>
>   <description/>
>   <content:encoded><![CDATA[asdf]]></content:encoded>
>   <excerpt:encoded><![CDATA[]]></excerpt:encoded>
>   <wp:post_id>1705</wp:post_id>
>   <wp:post_date>2016-12-23 01:03:44</wp:post_date>
>   <wp:post_date_gmt>0000-00-00 00:00:00</wp:post_date_gmt>
>   <wp:comment_status>open</wp:comment_status>
>   <wp:ping_status>open</wp:ping_status>
>   <wp:post_name/>
>   <wp:status>draft</wp:status>
>   <wp:post_parent>0</wp:post_parent>
>   <wp:menu_order>0</wp:menu_order>
>   <wp:post_type>post</wp:post_type>
>   <wp:post_password/>
>   <wp:is_sticky>0</wp:is_sticky>
>   <category domain="category" nicename="uncategorized"><![CDATA[Uncategorized]]></category>
>   <wp:postmeta>
>     <wp:meta_key>_edit_last</wp:meta_key>
>     <wp:meta_value><![CDATA[1]]></wp:meta_value>
>   </wp:postmeta>
> </item>

So just for the preview WordPress created the draft post. In our case ?p=1705, means the post ID will be 1705.

Hope you like this explanation.

In WordPress, there are no PHP sessions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.