Warning: Creating default object from empty value in /home/design/public_html/wp-content/plugins/paged-comments/paged-comments.php on line 31
WordPress Hook for Entire Page Using Output Buffering · Dagon Design

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. I absolutely love your blog and find many of your post’s
    to be just what I’m looking for. Would you offer guest writers
    to write content available for you? I wouldn’t mind writing a post or elaborating on many of the subjects you write related
    to here. Again, awesome weblog!

    web site [Lilla]

  2. Get focused and devoted and locate a spot where you can play daily, whether with
    peers or other footballers. It is Tennessee’s
    Rocky Top, Florida’s Old Ball Coach, and of course the Gator Chomp and
    the mighty Tim Tebow. Ole Miss and Mississippi State moving the Egg Bowl away from Jackson, Miss.

  3. Answer:By?linzed Espresso machines are raelly inexpensive nowadays. I would hate for you to buy someone else’s headache. I think I bought mine a few years ago for $ 100 and saw the SAME one at Walmart for like $ 20, I about croaked! LOL. Anyways, I honestly would just buy one new. They have come down considerably in price. If you still think you may want to buy it, make sure you price the replacement part (and the shipping), you may be unpleasantly surprised. Overall, just make sure it has its basic unit, and it does not smell burnt electrical wise, its carafe, and its espresso basket and screen. I would also make sure the cord is not frayed. Good luck!

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

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

  6. 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/

  7. 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/

  8. 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/

  9. The blog is absolutely fantastic

    breville je98xl juice fountain plus

  10. thanks for sharing the informative post.

  11. 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.