Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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>
share|improve this question
    
Am I missing something or wasn't there a related question to this already? –  hakre Nov 24 '10 at 15:01
    
@hakre: I restated the question once I got down to the crux of the problem ;) –  Scott B Nov 24 '10 at 15:13

1 Answer 1

up vote 3 down vote accepted

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.

share|improve this answer
    
Thanks Hakre. I'm going to accept your answer. –  Scott B Nov 28 '10 at 15:35

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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