formats

"Recently Popular" WordPress Plugin

Published on December 3, 2008 by in Projects

Download the Recently Popular plugin

I’ve used a few different plugins to manage the most popular post display on my sidebar. None of them behaved quite the way I wanted. The two major groups and their issues for me were:

  • Post counts last forever: This is a big deal if an article gets Dugg or Slashdotted. That post may live at the top of your most popular list long past its useful lifespan.
  • Formula-based: These use some formula of views, comments, and who-knows-what-else to try and derive what is most popular. These also count things into perpetuity, but generally they discount dated items as they age. The fact that views aren’t dated is still a problem.

I knew what I wanted. I wanted a popularity meter that I could tell “Show the posts that have been most viewed over the last 7 days.” Or 3 weeks. Or 12 hours. Or whatever. However I couldn’t find it. So I wrote a plugin (with widget!) to do just that. It records hits to any post or page on your site by any user who isn’t logged in, then allows you to display the most active ones for your defined time period on your sidebar.

Recently Popular Widget 0.6 (simple)

Recently Popular Widget 0.6 (simple)

Recently Popular Widget 0.6 (formatting options)

Recently Popular Widget 0.6 (formatting options)

Recently Popular Widget 0.6 (category options)

Recently Popular Widget 0.6 (category options)


The widget can be added to your sidebar more than once, so if you want to show the most popular over the last five days and the most popular over the last month you can.

Output formatting

The widget supports formatting strings for items in the results. These formatting strings accept HTML, so you can be as creative as you want. The template supports the following tags:

  • %post_title% – the post’s title
  • %post_excerpt% – the post’s excerpt
  • %post_url% – the post’s permalink
  • %hits% – the number of qualifying views
  • %display_name% – the post’s author
  • %user_url% – the post author’s URL
  • %publish_date% – the post’s publish date
  • %category% – the categories the post is in
  • %thumbnail_url% – the URL for the post’s thumbnail

To simply show the post titles as links to the post you would use this tag (note: this is the default output format):

<a href="%post_url">%post_title%</a>

The widget defines this and two other typical tags that you to simply click on and use without having to figure out the templating.

Using in Themes Without Widget Support

To install this plugin in a theme that doesn’t support widgets you add something similar to this to your template:

<ul><?php
  $rp = new RecentlyPopular();
  $rp->get_counts();
?></ul>

The get_counts() function takes a single parameter that is an array of the options. The options and their defaults are (default options in bold where applicable):

Option Values
Description
title ‘Recently Popular’
Title for the emitted ul. Primarily used by the widget.
enable_categories true
false
Determines if category filtering is used.
categories n/a
Category names in single quotes, separated by commas. Ignored if enable_categories is false.
date_format ‘Y-m-d’
PHP date format to use for date output tags.
default_thumbnail_url n/a
URL to a default thumbnail image to use when no thumbnail is found. Prevents empty images from showing.
display true
false
If true the call will emit an unordered list, if false it will return that list from the function call.
interval_type ‘HOUR’
‘DAY’
‘WEEK’
‘MONTH’
‘YEAR’
The type of interval to use.
interval_length 1
Integer value for the number of interval_type to display.
limit 10
Integer value for the number of results to display.
max_length 0
Integer value for truncating the title. 0 means no limit.
max_excerpt_length 0
Integer value for truncating the excerpt. 0 means no limit.
output_format %post_title%
String defining how each result will display. See Output Formatting above.
post_type RecentlyPopularPostType::ALL
RecentlyPopularPostType::PAGES
RecentlyPopularPostType::POSTS
The type of results to get.
user_type RecentlyPopularUserType::ALL
RecentlyPopularUserType::ANONYMOUS
RecentlyPopularUserType::REGISTERED
The type of results to get.

For example, this will show the five most viewed posts (without pages) in the past three weeks by users who weren’t logged in:

<ul><?php
  $rp = new RecentlyPopular();
  $rp->get_counts(array(
    'limit' => 5,
    'post_type' => RecentlyPopularPostType::POSTS,
    'user_type' => RecentlyPopularUserType::ANONYMOUS,
    'interval_length' => 3,
    'interval_type' => 'WEEK',
  ));
?></ul>
 
 Share on Facebook Share on Twitter Share on Reddit Share on LinkedIn
413 Comments  comments 

413 Responses

  1. [...] original here: "Recently Popular" WordPress Plugin | Eric Biven’s Blog This entry was posted on Wednesday, December 3rd, 2008 and is filed under WordPress Hosting. You [...]

  2. Adrian

    The issue with WP-Postviews that displayed pageviews for each article is that it counted page reloads by the same user in the same session, and didn’t ignore admin views.

    Maybe it’d be easier to hook into the Wassup plugin:
    http://www.wpwp.org

  3. @Adrian: Thanks for the heads up and I’ll certainly be looking to Wassup for inspiration, just like I did WP-Postviews. I considered plugging into an existing stats package but abandoned it for a couple of reasons.

    First, which stats package(s) would I work with? If I chose one I would severely limit the potential users. If I worked with more than one it would add a great deal of complexity and overhead to the code. It also then relies on others for the data to be correct.

    Second, not all stats packages can be tied in to. I personally use a log-parsing stats package locally and Google Analytics, neither of which would be convenient or reasonable for the widget to use as a data source.

    In the end the data storage needs for this plugin are very, very modest. The table has two columns: the timestamp of the hit and the page that was hit. It seemed silly to require anything more just to use this plugin.

  4. Огромное спасибо за потрясающие идеи!!! Буду следить за блогом, много всего интересного. А мой блог о науке, надеюсь, тоже понравится ;)

  5. Thanks for the plugin, was looking for it for a while

  6. Hey bro,

    How I install the plugin or how to show it in sidebar if my theme isnt widgetized ?

    please sos!!!!

  7. ross

    Hi!
    How can I configure your plugin to only show recently popular posts? Now its showing popular pages aswell.
    Great plugin!

    /ross

    • @Ross:

      That feature is in the works. I had begun work to add that as a configuration on the settings page (which is also in a future release), but now that the widget can be added multiple times it makes more sense for that setting to be on the widget. Hopefully I can get that moved over this weekend and possibly do a 0.3.x release.

      Glad you like it.

  8. 0.4 has been released. The widget was altered in this version to allow the user even more flexibility in choosing what items to count. Get it from the link at the top of this page.

  9. ross

    Thank you for the 0.4 update, works like a charm!

  10. Prabhu

    I pasted the line inside wordpress\wp-admin\includes\template.php

    But I am unable to see Popular count in the home page and also in the admin panel. I am currently using revoultionary theme2.0. Does this have an effect ? Kindly help.

  11. Thanks. This is the plugin I’ve been looking for a long time.
    Works perfectly – Great stuff!

  12. perke

    Hi Eric,
    thank you for a great plugin!

    I’m already using WP_postviews but really want to add timespan function and your plugin solves that…but, I was wondering is there any way we can split this by categories (like, popular from category) ? As I’m already using this method for the site (www.tip.ba) with help of wp_postviews template function, php_exec and slayer’s custom widgets plugins(messed, huh).

    Having a widget option or template function for category breakdown would be great and I would certainly donate to have it!

    • @perke:

      That’s a fantastic idea. Consider it done.

      I have a release that should go out today or tomorrow that adds formatting to the output so that you can choose how the items in the list look. I’ll work on this next.

  13. Using WP2.7, and doing an automatic install, I can’t find any of the configuration panes contained within the screenshots. There is no “Recently Popular” widget to choose from.

    Any ideas?

    • @Mike:

      When you install the plugin there will be two plugins to enable under “Plugins -> Installed”. The first is the base “Recently Popular” which does the data gathering and supports non-widgetized themes, the second is the “Recently Popular Widget”. The widget is split out so that it doesn’t have to load for people who don’t have widgetized themes. If you’ve activated both of them and the widget still doesn’t show up then let me know what OS, web server, and PHP version you’re using and I’ll see what I can do.

  14. perke

    Great news Eric!
    Can’t wait to see it in action :)

    Thanks a bunch!

  15. Hello

    One question, will it work with super-cache plugin installed?

    Thanks again for your great plugin.

    • Yes, it works perfectly with the WP Super Cache plugin, I use it in conjunction with Recently Popular on this site. It will correctly record all page views. The one thing to be aware of is that any cached page can’t have completely updated counts since it is, by definition, serving cached data.

  16. Fantastic plugin! Does just about everything I’m after. As perke mentioned, the ability to specify a category for each iteration would be greatly appreciated.

    I’ve tried several “popular posts” plugins over the past week and yours comes closest to what I’m after. If you added category functionality, it would be perfect.

  17. That’s great news, Eric. Thanks! The client project I’m working on launches on the 10th of January, so that will be just in time.

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>

© All Content Copyright Eric Biven
credit