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.

I want pass current user cookies in wp_remote_get function to get a Draft Post Preview page content.

I check already the questions:

But in either of them appears how I can get the cookies to the pass it to wp_remote_get function.

I assume I can do what I want passing the cookies to wp_remote_get function, here in WordPress documentation mention how WordPress stores the cookies but how can I get them taking in care they use a hash value in the cookie name?

What I want to do is given a Post ID get the content of the WordPress Post view page and analyse it. For the already published Posts all works fine, but for Draft Posts I get that the page doesn't exist. Here is the code simplified:

(...)
$post_permalink = get_permalink($post_id);
$response = wp_remote_get($post_permalink);
$whole_post_page = $response['body'];
(...)

This code is executed when the owner of the Post is editing it, so the user is already authenticated and the request is for a local Post. How I can accomplish that wp_remote_get returns me the Post Preview page content? as WordPress does when I go to the preview link in my browser.

Thanks in advanced.

share|improve this question

2 Answers 2

up vote 3 down vote accepted

I rarely deal with cookies and not sure about complete mechanics there, but here is basic working example of passing current user's cookies to retrieve preview page source:

$preview_link = set_url_scheme( get_permalink( $post->ID ) );
$preview_link = esc_url( apply_filters( 'preview_post_link', add_query_arg( 'preview', 'true', $preview_link ) ) );
$cookies      = array();

foreach ( $_COOKIE as $name => $value ) {
    $cookies[] = new WP_Http_Cookie( array( 'name' => $name, 'value' => $value ) );
}

$request = wp_remote_get( $preview_link, array( 'cookies' => $cookies ) );
$body    = wp_remote_retrieve_body( $request );
share|improve this answer
    
Works perfectly, thanks! –  leticia Mar 8 '13 at 19:20

What's the scenario here, you're trying to retrieve a draft post preview from your current installation, located on another, right?

Are you authenticating yourself prior to retrieving?

I'm thinking that you would need to use,

wp_signon($credentials, $secure_cookie); $secure_cookie is boolean

Then you might need to also run,

wp_set_current_user($user_id);

With wp_set_current_user possibly being needed to actually access the preview (?). I'll say I'm not 100% sure on that, but I believe its necessary for the post edit screen, so being an extension of that, one would assume so. Haven't had a chance to look around core files yet for confirmation.

That should set the cookies for you and allow you to view your preview, which means you theoretically don't have to pass anything into the cookie array parameter as they're already set.

Lets see your code too!

share|improve this answer
    
Thanks, I update the question with the information you ask me. –  leticia Oct 12 '12 at 17:26
    
The functions referenced doesn't help me because the user is already logged and I want use its cookies to get the Preview of a Draft. –  leticia Oct 22 '12 at 22:18

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.