Welcome to Dagon Design. In addition to free scripts, WordPress plugins, and articles, we offer a variety of services including custom theme design, plugin creation, and PHP scripting. Contact me for more information.

Updated Thursday, February 19th, 2009 at 2:14pm

WordPress Hook for Entire Page Using Output Buffering

The WordPress plugin API provides a large number of action and filter hooks to make plugin creation easier. While the provided hooks cover just about everything you would need for plugin development, it can occasionally be useful to have a hook that can process the entire html output of WordPress. This article describes a method that can be used to create a ‘virtual’ hook which allows processing of everything between the end of the header section, and the footer of the theme, using the output buffering feature of PHP.

Plugin Code

function callback($buffer) {
	// modify buffer here, and then return the updated code
	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');


This plugin code registers two actions – buffer_start and buffer_end.

buffer_start is executed at the end of the header section of the html. The parameter, the callback function, is called at the end of the output buffering. This occurs at the footer of the page, when the second registered action, buffer_end, executes.

The callback function is where you add your code to change the value of the output (the $buffer variable). Then you simply return the modified code and the page will be displayed.


Be sure to use unique function names for buffer_start, buffer_end, and callback, so they do not conflict with other functions you may have in plugins.

For more information on output buffering in PHP, visit the PHP page for the ob_start() function.

  If you have found this page useful, please consider donating. Thanks!