Popularity Contest for Pages

I’ve been enjoying Alex King’s excellent Popularity Contest plugin for several weeks now. It’s fascinating to see which posts are being viewed, commented, tracked and pinged, all through the one interface. After using it for a while, I thought of a useful way to extend it and also found and fixed a wee problem. Perhaps these changes will make their way into the next version of Alex’s plugin, but till then you can always download my version from here.

Count Pages

The first extension was to include WordPress Pages as well as Posts in the statistics. Some people don’t use Pages at all, but I use them a lot on Semicolon; in particular, I use them for my WordPress plugin homepages, which are some of the most popular pages on the site. I wanted them to be included in the popularity contest, so I made the following changes to Popularity Contest:

To make sure Page views are tracked, I changed if (is_single()) to if (is_single() || is_page()) in the tracking code. Nice and easy.

To make Pages appear in the reports, I changed the SQL code in various places from post_status = 'publish' to post_status IN ('publish','static'). This includes Pages, which always have status set to ’static’. I didn’t change this everywhere, because some reports should not have Pages included — Pages do not fall within the normal chronology of Posts, so should not appear in date-based reports.

Conflict with other plugins

A couple of weeks after I installed Popularity Contest, it suddenly stopped working — the statistics were no longer being updated. I eventually figured out that Popularity Contest is incompatible with the Google Sitemaps plugin, which I installed at that time. The Google Sitemaps code includes a WordPress admin include file for some of its function. Unfortunately, Popularity Contest checks for the existence of an admin function to decide whether a view is coming from an admin page. Therefore, with Google Sitemaps enabled, Popularity Contest always thinks it’s on an admin page and therefore never updates its stats.

The fix is simple; delete Popularity Contest’s is_admin_page() function and use WordPress’s built-in is_admin() function instead. This function effectively checks whether the URL contains “wp-admin/”, which as Alex says is not the best idea:

Initially, I just did a check in the URL for wp-admin, but that’s really not a good solution. I had an IM chat with Dougal and he first suggested checking for get_bloginfo(’wp_uri’).’/wp-admin/’, but we decided that sub-domains could be problematic there. Then he suggested checking of the existence of a function from admin-functions.php. This seems like the best idea to date and it will be in the next release of the plugin.

The problem with this approach is shown up by the Google Sitemaps conflict — there’s nothing to stop another plugin from including admin-functions.php, which will break the admin page detection. I feel it’s best to use the built-in WordPress function — it’s less likely to cause obscure incompatibilities, simply because it’s in the WordPress core.

Download

Finally, my version of Popularity contest switches off the default popularity display per post. I like it like that.

Download the tweaked version of popularity-contest.php. To install, just copy it over your existing version. My modifications to popularity-contest.php are in the public domain. Share and enjoy.

Share This | Comments | Permalink | Trackback | Comments feed

21 responses

Other comment pages: « 1 [2]

  • Thanks a lot, was looking for something like that indeed.

    anyway to filter out the categories of pages? I know they are attached a category in the wordpress database. but they shouldn’t be having any actually.

    TedFox | 14 February 2006
  • I think filtering out pages from the reports would be a bit tricky — something for Alex to do rather than me. :)

    Bennett | 17 February 2006
  • [...] Second, I have installed the Popularity Contest plugin by Alix King. Simply put, it tracks the number of reads and allows articles to be ranked [...]
  • Thank you. It works great. The orginal verision wasn’t working for me so I thought I try yours and it finally starting working. Awesome!

    Tim | 28 April 2006
  • [...] Heute nun habe ich einen Beitrag von Bennett im semicolon-Blog gefunden. Dort beschreibt er die Ursache für dieses Problem im Zusammenhang [...]
  • Hi…I terribly need help. I am getting this error message when I try installing it.

    Table ‘manilada_wrdp1.wp_ak_popularity_options’ doesn’t exist

    What does this mean?

    Anne | 11 July 2006
  • Well, Anne, when you install Popularity Contest, it is supposed to create a couple of new database tables. Apparently yours has failed to do this for some reason. Unfortunately I don’t know the best way of diagnosing or fixing the problem. You could try asking Alex via the original Popularity Contest page.

    Bennett | 12 July 2006
  • [...] Auf meiner Suche nach einer Lösung stiess ich aber irgendwann nicht mehr nur auf Debian Popularity Contest, sondern auch auf folgende Hilfeiche[...]
  • I just want to THANK YOU for editing this code. I have been looking (for WEEKS) for how to include static pages in the code and have the stats actually work. You are a Wonderful, Wonderful, Awesome, Kick-Heiny Person, and I can’t Thank you enough.

    Have a Great Day.

    Liss | 30 July 2006
  • Thanks for your kind words, Liss! :) I’m really glad you found it useful. Of course I am standing on the shoulders of giants here — the real thanks go to Alex for writing the original plugin!

    Bennett | 30 July 2006
  • Nice work on this, thanks :-) However anytime I try to update popularity values or reset the comments count I get this error

    “You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ” at line 2″

    Any thoughts?

    Fraser | 21 September 2006
  • Ignore me, sorry. I had uploaded the plugin to

    /wp-content/plugins/popularity-contest/

    instead of just at the root of the plugins folder. It means it half works but certain things don’t :-)

    Thanks for this adaption.

    Fraser | 21 September 2006
  • I get an error trying to create tables on initialisation (same problem as Anne):

    Table ‘cod_blog.wp_ak_popularity’ doesn’t exist

    Any ideas?

    David Bovill | 25 September 2006
  • Hi thanks - even with with the fixes to Alex’s script you post here I am still getting the following errors when I activate the plugin:

    Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/.goethe/fortyfoxes/cod.architex.tv/wp-content/plugins/popularity-contest.php on line 263
    Table ‘cod_blog.wp_ak_popularity’ doesn’t exist

    Then when I look at the reports:

    WordPress database error: [Table 'cod_blog.wp_ak_popularity' doesn't exist]
    SELECT p.*, pop.* FROM wp_posts p LEFT JOIN wp_ak_popularity pop ON p.ID = pop.post_id WHERE p.post_status = ‘publish’ ORDER BY pop.total DESC LIMIT 50

    Can I create the tables manually? Alex does not seem to be supporting the plugin… aybe Anne fixed her problem? Help appreciated!

    David Bovill | 26 September 2006
  • Trackback | Bob the Chicken Killer » Popularity Contest | 28 October 2006
    [...] Downloaded it from this site and it sure worked well. I am glad that this tweaked version worked well. [...]
  • Alex has released version 1.2 of the plugin. Do you know if the update fixes the conflict with other plugins? Since you are the one who initially brought up the problem, I thought you might know.

    Ken | 5 November 2006
  • Ken, it looks as if the new version does fix the conflict — it uses the built-in is_admin() function if it’s available.

    Bennett | 6 November 2006
  • I get this error when I try to post. Any ideas? Thanks!

    Invalid argument supplied for foreach() in wp-content/plugins/popularity-contest.php on line 1290
    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘)’ at line 3

    David Paul Robinson | 6 April 2007
  • it seems that this plugin it doesn’t work with the newest wordpres versions 2*

    Carlos | 10 August 2007
  • Thanks for the tweak.

    larsen | 6 September 2007

Other comment pages: « 1 [2]

Post a comment

(required)
(required)
(some HTML allowed; for literal < and &, use &lt; and &amp;)
Close
E-mail It