In this article tripwire magazine provides a mini-tutorial on how to setup a Most Popular Posts Widget in WordPress (hit based). In my opinion it is a good feature to have on a blog as it may trigger visitors to click to see a few more articles. At the same time I found that it is not that simple and out of the box to create it for WordPress as I was expecting. I managed to get a working solution for tripwire magazine that I’m still testing and keeping an eye on, but I think it is stable enough to share with you.
I’m still not a WordPress power user but I have concluded that wordpress does not offer the functionality needed to create a Most Popular Widget out of the box. If I’m wrong please drop a comment to make my solution simpler. If I’m right let me know if you think the solution I puzzled together today is OK.
Other relevant resources
Before you proceed to the tutorial consider if one of these articles could interest you!
Getting your hands on a working plugin…
First of all it is hard to find a plugin that works with the latest version of WordPress (2.7.1). The way plugins are structured and labeled for compatibility with specific versions of WordPress is just not good enough in my opinion. I know it is hard to control this as versions come and go and plugins are developed by the community. On the other hand I worked quite a lot with Joomla and I think it really works well with components, modules etc. for Joomla. There are two major releases for Joomla 1.0.x and 1.5.x and extensions made for ex. 1.5.x normally works fine on Joomla 1.5.10 and all previous patch releases. Drupal also does a great job and have almost adapted what I would call best practice Configuration Management for a Open Source project. See how every module is marked for compatibility with specific releases of Drupal and how the community can submit bugs and feature requests to the developer…really nice!
After some searching and trying out different plugins I found KF most read that is listed as WordPress 2.7.1 compatible. This module almost gives you what you need. Getting the output of KF most read into a widget required another plugin as it is not build as a widget plugin. I found PHP Code Widget very useful for this purpose.
Step 1. Get the files.
KF most read: Download
PHP Code Widget: Download
Step 2. Correct minor error in KF most read
After enabling the plugin it creates a new table in the database and starts collecting post hit data. Every hit on a post is registered in the new table as a new record. I’m currently a bit unsure if this is the right approach and how it will work when data really builds up. Anyway this is not the problem you need to solve to make use of the plugin downloaded from wordpress.org. After installing it I went to the database and saw that all records had a zero in the post_ID column… hmmmm. Looking at the code that inserts the record immediately exposed the problem. The variable $post_id that where used to insert the post id where never assigned any value. Inserting the followin line of code solved this issue:
//Inserted to fix use of unassigned variable.
3. Install and activate both plugins.
This is not a tutorial on how to install plugins in general but it is very simple.
a. Upload the files under your wp-content/plugins directury
b. Log into WordPress back-end and click the Plugins menu button. Find the 2 new plugins under Inactive Plugins and press Activate.
4. Add new widget as placeholder for your Most Popular Posts list.
Still being logged into WordPress back-end click on the Widgets menu button. On the Widgets page a new type of widget is now available called PHP Code.
Press Add and a new instance will be Added to the list of active widgets on the right. Press edit and paste in the following code as shown on the image below.Type in a Widget Title and Accept and Save.
<?php echo kf_get_posts_by_hits (7, 10);?>
The function echo kf_get_posts_by_hits ([time window in days],[number of posts]) takes to parameters. Using 3,7 as in my example above basically means
“retrieve the 10 most read posts in 7 days”.
The result on tripwire magazine looks like this. I’m sure you can customize it to fit your needs for Look & Feel as this example should give you a good start.
I hope you found this mini-tutorial useful. I would really appreciate if you post a comment and/or bookmark it, thanks.