Popularity Contest for Pages

Thursday, 22 September 2005

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.

Tags:

21 comments

You can leave a comment, or trackback from your own site.

  1. I thank you very much, indeed! The plugin didn’t work until I’ve read your hints about some conflicts with the Google Sitemaps plugin. I’ve made the changes and everything works fine.
    All the best, Peter

  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.

  3. I think filtering out pages from the reports would be a bit tricky — something for Alex to do rather than me. πŸ™‚

  4. 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!

  5. 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?

  6. 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.

  7. 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.

  8. 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!

  9. 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?

  10. 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.

  11. 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?

  12. 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!

  13. 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.

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

  15. 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

  16. it seems that this plugin it doesn’t work with the newest wordpres versions 2*

Leave a comment