Buffering WordPress rendered page

if( current_user_can('manage_options') )
{
add_action('template_redirect', 'YOURPLUGIN_buffer_start', 0);
}

function YOURPLUGIN_buffer_start() {
add_action('shutdown', 'YOURPLUGIN_buffer_stop', PHP_INT_MAX);
ob_start('YOURPLUGIN_modify_content');
}
function YOURPLUGIN_buffer_stop() {
ob_end_flush();
}
function YOURPLUGIN_modify_content($content) {

$dom = new DomDocument();
$dom->loadHTML($content);
$styles=array();
foreach ($dom->getElementsByTagName('link') as $node)
{
if($node->hasAttribute('rel') && $node->getAttribute('rel')=='stylesheet' )
{
$styles[]=$node->getAttribute("href");
}

}

//modify $content
return serialize($styles).$content;
}

Leave a Reply

Your email address will not be published. Required fields are marked *