Get Post or Page Contents as a PHP Variable

Posted on February 5th, 2011

Here is a quick snippet for placing all post or page content into a variable. We’re talking the entire page contents here, not just the content of the post. Place this code into your theme’s functions.php file:

// post contents as variable
function callback($buffer) {
    return $buffer;
    }
function buffer_start() {
   ob_start("callback");
    }
function buffer_end() {
    ob_end_flush();
   }
add_action('wp_head', 'buffer_start');
add_action('wp_footer', 'buffer_end');

Once in place, this function will capture the entire page contents into a variable called “$buffer”. You may then do whatever you wish to this variable. Filter it, match it, slap it around a little and show it who’s boss. That sort of thing.

Here is a quick snippet for placing all post or page content into a variable. We’re talking the entire page contents here, not just the content of the post. Place this code into your theme’s functions.php file:

// post contents as variable

function callback($buffer) {

return $buffer;

}

function buffer_start() {

ob_start(“callback”);

}

function buffer_end() {

ob_end_flush();

}

add_action(‘wp_head’, ‘buffer_start’);

add_action(‘wp_footer’, ‘buffer_end’);

Once in place, this function will capture the entire page contents into a variable called “$buffer”. You may then do whatever you wish to this variable. Filter it, match it, slap it around a little and show it who’s boss. That sort of thing.