In the code below, I’m attempting to get a reference to the body content of the current post’s fully rendered preview…
$response = wp_remote_retrieve_body(wp_remote_get('http://localhost/mysite/test-post/?preview=true&preview_id=28&preview_nonce=640bc54ca4'));
$post->post_content = $response;
I’m just sending it to $post->post_content so that I can easily see it.
Here’s part of what it returns…
<body id="error-page">
<p>You do not have permission to preview drafts.</p></body>
</html>
I’m logged in as the administrator, so obviously I’m calling the preview wrong. How can I pass the URL in wp_remote_get to obtain the body section of the preview stream?
When I use…
$post->post_content = "<pre>".$response['body']."</pre>";
I get
<pre><</pre>
The URL is okay, what you need to add are cookies that authenticate you as the user who is allowed to see the preview. That is basically sending headers. I would start with the HTTP API (WordPress Codex) looking for a method to add additional HTTP headers and set your cookies.
Otherwise – because probably this is somewhat complicated – you could take a look inside the preview code so to see if you can provide a hook to control access to previews. If possible, you can add some secret parameter to the URL which would allow you to preview any page w/o the need of being logged in (and the need of a nonce as it needs to be provided for preview as well).
The result of either the first or the second approach is quite the same, the only difference would be how to trigger it.