When I write a post and schedule it for later publishing, WordPress will allow me to preview the post as if it was already published but I need to be logged in as administrator (or anyone who is authorized to preview the post).
Is there a way, possibly a query string parameter, to make the post previewable by anyone, i.e. also by anonymous users? Something like my-post?previewsecret=645732116468
?
Draft previews
Take a quick look at this chunk of core code in query.php which
http://core.trac.wordpress.org/browser/tags/3.3.1/wp-includes/query.php#L2658
…is what makes it somewhat non-straightforward to bypass for non logged in users. You could override the result of that function by going deeper and then taking care of roles. But that’s too much overhead and can contribute to exposed security.
If you look a little further up http://core.trac.wordpress.org/browser/tags/3.3.1/wp-includes/query.php#L2632 on line 2632 you’ll find that the post is actually fetched from the database and can be further filtered using the
posts_results
hook that comes a couple of lines lower.Store the value of the post, and inject it towards the end after all the checks that void the
posts
array. http://core.trac.wordpress.org/browser/tags/3.3.1/wp-includes/query.php#L2740 wherethe_posts
hook is eagerly awaiting.So something rough would look like:
Append your post preview link with the secret key
?p=4601&key=foryoureyesonly
and the post is displayed for anyone. The code has some dirty going-ons, like the stash, you can useglobals
instead (not recommended), or wrap it up into an object (yes!) and further extend the functionality with custom passwords for each post, etc.Scheduled previews
Scheduled previews work in a very similar way. The
posts_results
are populated with'future'
status posts in absolutely the same fashion and the same hooks can be leveraged. The code will work without change for scheduled posts./2012/07/12/one-two-three/
–404 NOT FOUND
/2012/07/12/one-two-three/?key=foryoureyesonly
–200 OK
Soulseekah’s answer is incredible, and the exact thing I was looking for. In searching for this, I also came across a plugin (that might be based off this post for all I know).
Public Post Preview