I am trying to build a widget based on the default wordpress recent posts widget. In wp-includes/default-widget.php , I noticed in class WP_Widget_Recent_Posts in function widget(), the recent posts are first looked for in cache and if not found, then output buffering is turned on and the result is generated and then stored back into the cache. Since I am using NuSphere IDE , I put a DebugBreak(); inside the condition if ( isset( $cache[ $args['widget_id'] ] ) ) (DebugBreak() causes a breakpoint exception to occur in the current process.) but the breakpoint was never executed pointing out that the cache wasn't set before. Now this shouldn't be the case as the cache does get set by wp_cache_set('widget_recent_posts', $cache, 'widget');. This is my first dilemma.

Secondly, I would really appreciate if someone threw some light on how output buffering works. Cause as I mentioned before, the recent posts are first looked for in cache and if not present there then output buffering is turned on (ob_start();), result is fetched and then ob_get_flush(); is performed like $cache[$args['widget_id']] = ob_get_flush(); and the cache is set. So if no output is echoed to the browser, then how does the widget actually display the recent posts? PLEASE HELP!

1 Answer 1


Unless you have a memcached-type plugin installed, wp_cache_set will only store data for the duration of the current script. Call or add the widget again in the same instance & you'll see it utilise the cache.

As for ob_get_flush(), taken from the manual:

Flush the output buffer, return it as a string and turn off output buffering

In other words, it prints and returns the buffer at the same time.

  • Got it! Called the widget() twice in a loop and the cache worked :) . And I didn't reckon that flush output buffer = print output buffer. I simply interpreted it as clear output buffer. Everything is crystal clear now. Thanks! :) Commented Jul 18, 2012 at 14:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.