Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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!

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

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.

share|improve this answer
    
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! :) –  Poulomi Nag Jul 18 '12 at 14:41
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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