Howto: WordPress 2.7+ Most Popular Posts Widget

April 19, 2009 · 13 comments

by Lars

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.


ElegantThemes
ThemeForest

Advertisement

Introduction

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!

75+ Excellent Free Fonts For Professional Design

100+ High Quality Hand-picked Icon Sets

In this article tripwire magazine provides a collection of more than 100 High Quality Hand-picked Icon Sets that you can use in your designs. The article has been organized into 16 sections giving you instant access to the Icon Sets you need. The power of really good Icons can’t be underestimated as they, if used wisely, will serve as accelerator for creating really beutiful designs quickly!

75+ Excellent Free Fonts For Professional Design

75+ Excellent Free Fonts For Professional Design

In this article tripwire magazine presents an overview of more than 75 excellent free fonts you should consider using for your designs. Not all free Fonts are worth using but the Fonts listed in this article have all been picked out because they are better than the rest.

113

100+ Massive CSS Toolbox

In this article tripwire magazine provides a Massive CSS Toolbox giving you access to a really large collection of CSS Tools, Tutorials, Cheat Sheets etc.

92

35+ very Useful And Powerful CSS techniques

In this article tripwire magazine provides a list of very useful CSS Techniques that you can use freely to make your website or blog more dynamic and inprove its Look & Feel. The article will also give you an introduction to CSS and why it should be used instead of table-based layouts. hand-picked because they deserve to.

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.
$post_id= (int)$post->ID;

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.

<ul>
<?php echo kf_get_posts_by_hits (7, 10);?>
</ul>

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.

{ 6 comments… read them below or add one }

cheap custom jerseys May 19, 2011 at 10:42 am

Howto: WordPress 2.7+ Most Popular Posts Widget

Reply

widget June 1, 2010 at 4:39 am

nice widget, i hope i can use it, good article, thank you for it

Reply

Bati April 19, 2009 at 3:11 am

Really a long post.The information is useful for everyonewho used Wordpress.Thanks for post

Reply

Liz Hover April 13, 2009 at 6:38 pm

Thanks for posting this tutorial. It totally works and makes absolute sense and I’m no WordPress power user either. I numbered my posts and am trying to also add a line between each post title to make it easier to read. Cheers for this.

Reply

tripwiremag April 13, 2009 at 7:04 pm

Thanks for your feedback Liz. I just looked at your blog and it looks great with a Most Popular Posts widget ;))

Reply

Angger Atmawarin April 12, 2009 at 10:54 pm

Bookmarked.
Thanks for sharing this dude. I think I’m gonna use it for my site.

Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

{ 7 trackbacks }

Previous post:

Next post:


Web Analytics