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! 🙂

Related Articles

2 COMMENTS

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Try EchoTools - my free, iOS ultrasonography reference application!

Latest Articles