Skip to content

Instantly share code, notes, and snippets.

@retgef
Created December 31, 2012 13:26
Show Gist options
  • Save retgef/4419756 to your computer and use it in GitHub Desktop.
Save retgef/4419756 to your computer and use it in GitHub Desktop.
Allows you to send the current user auth cookie along with a WP HTTP API remote request. This is useful for storing a page load for analysis in a psuedo-buffer if you are competing with cache plugins that use output buffering.
<?php
#Set your current WP install URL
$url = 'http://currentwpinstall.com';
#Create cookie string
$cookie_string = '';
foreach($_COOKIE as $k => $v)
#Assure we are setting the proper string if other cookies are set
if(preg_match('/(wordpress_test_cookie|wordpress_logged_in_|wp-settings-1|wp-settings-time-1)/', $k))
$cookie_string .= $k . '=' . urlencode($v) . '; ';
#Remove stray delimiters
$cookie_string = trim($cookie_string, '; ');
#Prep headers
$headers = array(
'Cookie' => $cookie_string
);
#Make Request
$http = new WP_Http;
$response = $http->request($url, array('method' => 'GET', 'headers' => $headers));
#Retrieve Body
$body = wp_remote_retrieve_body($response);
@allysonsouza
Copy link

Very helpful, 10 years later!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment