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');

Explanation

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.

Notes

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!

Pages: [4] 3 2 1 » Show All

  1. it can occasionally be useful Very important Things and its an authentic ideas and plugins Visit my site for more info

  2. it can occasionally be useful to have a hook that can process the entire html output of WordPress

  3. This article is very useful, I have been looking for, thank you.Very good?http://brevillereview.net/reviews/breville-bje510xl-ikon-900-watt-variable-speed-juice-extractor/

  4. the above article is interesting for all concerned. For my info is really useful.http://brevillereview.net/reviews/breville-bje510xl-ikon-900-watt-variable-speed-juice-extractor/

  5. The post is written in very a good manner and it entails much useful information
    http://brevillereview.net/reviews/breville-je98xl-juice-fountain-plus-850-watt-juicing-machine/

  6. The blog is absolutely fantastic

    breville je98xl juice fountain plus

  7. thanks for sharing the informative post.

  8. Thanks for the wordpress hook. I was looking for something like this while working on my site.

Pages: [4] 3 2 1 » Show All

Leave a Comment

Before you comment: If you are having an issue with a script, please make sure you have read the entire article. Also, please read through the comments because most common issues have already been discussed many times. Thanks.


Be sure to wrap all code in <code></code> tags.