WordPress Cached Disqus Recent Comments Widget

Thanks to the Disqus Recent Comments Widget, WordPress users can easily display recent Disqus comments through sidebar widgets; however, for dynamic sites with significant activity, Disqus API requests can prove to be cumbersome. What if you exceed the allotted requests per hour? How slow will fetching recent comments make page loads? This is where WordPress’ Transient API comes in handy!

After installing the plugin, open wp-content/plugins/disqus-recent-comments-widget/disqus_recent_comments_widget.php. I recommend copying over the latest developmental code from here. Around line 250, add the lines below to the very beginning of the echo_comments function.

protected function echo_comments($comment, $api_key, $style_params,$args=false) {
	$cached = get_transient($SITEDISQUS);
	if (false !== $cached){echo $cached;return;}

Now scroll to around line 360 where the function ends. Add the set_transient code right above the final ‘echo’ output.

set_transient($SITEDISQUS, $recent_comments, 600);
echo($recent_comments);
}

Essentially, the modified function will check to see if your comments are stored in a transient ($SITEDISQUS). If yes, then the function will terminate early with the transient output. If the transient has expired (in the example above, ‘600’ refers to 600 seconds), the function will be executed, and the transient data will be refreshed for another 600 seconds.

Let me know if you have questions or if this helped! :-)

Legal

My posts are not to serve as a replacement for recommendations provided by licensed physicians nor do they represent the opinions of Baylor College of Medicine or its affiliated institutions. Please read this site's Disclaimer and Terms of Use for more information.

Comments

  • Giga

    That was exactly what i was searching!!! Thanks so much for this article!!!

    • http://rk.md/ Rishi

      Thanks for the comment, Giga! Hope the walkthrough helps! =D