:: BleakGeek.com ::

Wordpress Top10/View Count Plugin

Blogged in Wordpress, Wordpress Dev on Friday April 8, 2005

I released a ported version of Top10/View Count plugin about a month ago. I ported it over from the txt file that Laughing Lizzard had into a plugin so that it was easier to use. It had some bugs in it, so I decided to create some more documentation which will help make it easier to use now. Below are some instructions about how to use the plugin.

Download: Top10.zip

First off what I did was I use phpMyAdmin to select my Wordpress database. After doing that, I select SQL to insert some SQL data. This code is for the view count part of the script. The SQL data you need to enter is:

create table mostAccessed
(
postnumber int not null,
cntaccess int not null,
primary key(postnumber),
unique id(postnumber)
);

Next, I inserted the code into the Wordpress loop so that it may add each hit as it is viewed. This code is directly from my template and it will vary upon what your template is using, but this is the idea:


<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<?php if ($id > 0) { add_count($id); } ?>
<div class=”entry”>

<h3 id=”post-<?php the_ID(); ?>”>
<?php
<p class=”meta”>Posted by < ?php the_author(); ?> on < ?php the_time('F jS, Y') ?> — Posted in < ?php the_category(', ') ?>
<?php the_content('’); ?>

I used $id because I use permalinks for my site, if you are not using permalinks, aka index.php?p=XX, then use $p instead of $id. The was the hard part….wasn’t too hard, right? Next, I added in the code below to where I wanted the view count displayed:


<p class="postmetadata">< ?php edit_post_link('Edit','','<strong>|'); ?>
<?php comments_popup_link(’No Comments »’, ‘1 Comment »’, ‘% Comments »’); ?>
<a href=”<?php trackback_url() ?>” rel=”trackback”>Trackbacks (<?php print_r(count($trackbacks)); ?>)»
<?php show_post_count($post->ID, $before=”Views(”, $after=”)”); ?>

I put this code right next to my trackback counter as you can see. This code is all open source. I simply just put my site together from a template and plugins. If you need anymore help, feel free to comment here and I will be lad to help you fix your code. A reminder to all, this code is for Wordpress 1.5, but will possibly work in Wordpress 1.2. Thank you.

Ending thoughts: I would like to give many people credit for helping me fix this plugin, most of the fixed code was taken from Daynah. Thank you all.


Sponsors

Warning: Unknown(): open_basedir restriction in effect. File(/home/bleak/public_html/top10ads.php) is not within the allowed path(s): (/home/vhosts/bleakgeek.com/httpdocs:/tmp) in /home/vhosts/bleakgeek.com/httpdocs/wp/wp-content/plugins/phpexec.php(61) : eval()’d code on line 1

Warning: Unknown(/home/bleak/public_html/top10ads.php): failed to open stream: Operation not permitted in /home/vhosts/bleakgeek.com/httpdocs/wp/wp-content/plugins/phpexec.php(61) : eval()’d code on line 1

Warning: Unknown(): open_basedir restriction in effect. File(/home/bleak/public_html/top10ads.php) is not within the allowed path(s): (/home/vhosts/bleakgeek.com/httpdocs:/tmp) in /home/vhosts/bleakgeek.com/httpdocs/wp/wp-content/plugins/phpexec.php(61) : eval()’d code on line 1

Warning: Unknown(/home/bleak/public_html/top10ads.php): failed to open stream: Operation not permitted in /home/vhosts/bleakgeek.com/httpdocs/wp/wp-content/plugins/phpexec.php(61) : eval()’d code on line 1

Warning: (null)() [function.include]: Failed opening ‘/home/bleak/public_html/top10ads.php’ for inclusion (include_path=’.:’) in /home/vhosts/bleakgeek.com/httpdocs/wp/wp-content/plugins/phpexec.php(61) : eval()’d code on line 1

62 Responses to “Wordpress Top10/View Count Plugin”

  1. No GravatarReuben says:

    It works better if you put the add_count line in the single.php and search.php pages also.

    You should change the future plugin to accept a parameter for the # of posts to show, and whether to show counts. I hacked your plugin manually to do these.

    Good plugin tho! Thanks

  2. No GravatarJonathan says:

    Thanks for the suggestion. I’m glad I could help provide a good plugin, even though it was just ported over. Hope it serves you well :)

  3. No GravatarJens Wedin says:

    I have problem with the title is clipped after 20 characters. See my blog. The link “Swedish accesskey standard” is clipped. Do you know what the problem can be?

  4. No GravatarJens Wedin says:

    Sorry, look at the “KHTML on Windows part II”.

    Also another thing, would it be possible to add so some posts will be excluded from this. Look at my blog again. For some reason a lot of search engine robots hits my first post (I am a blogger…) I see no point of showing that one to the rest of the world. ;D

    Thanks for updating the plugin.

  5. No GravatarJonathan says:

    That’s a simple fix. Change the line

    $post_title = substr($post->post_title, 0, 20);

    change that 20 to whatever the length you want to have the the title. Take it and make it just 0) if you don’t want a length for it. Hope that helps.

  6. No GravatarJames Farmer says:

    Hiya,

    Great plugin… one of the best.

    My main use for this is to for a kinda ‘best of (apparently)’ element so I’m really interested in the ‘top 10′ function.

    Am having trouble for some reason with the URLs, each post link is rolling to the next one… will carry on playing & will report if I can fix it but any ideas about why this might be happening / what I can do to fix it would be wonderful!

    Cheers, James

  7. No GravatarJames Farmer says:

    I *think* I’ve got it by playing around with the plugin to get this for the link bit:

    A href=”index.php?p=”.$post->ID.” title=”$text”>$post_title /A

    Still not ideal as the title doesn’t come up… but will keep on pluggin

  8. No GravatarJonathan says:

    James, it looks like your code is a little tweaked. I think you missed a quote somewhere in the code there and need to add one in because you have title= in the actual URL instead of just href=”url” title=”". You may want to check that out and verify that the php is closed. Maybe paste me that line here and we can see if we can sort it out for you.

    Hope that helps.
    Jonathan

  9. No GravatarJonathan says:

    Sorry, missed your second post. I’m not using this exact code because I have permalinks, but this is what the code should be:

    echo “ID.”&more=1&c=1″ title=”$text”>$post_title

    Let me know how it goes.
    Jonathan

  10. No GravatarJens Wedin says:

    Jonathan: Thanks for your help. Works great now.

  11. No GravatarJonathan says:

    Glad it works. Feel free to drop another note if you need anything else.

    Blessings,
    Jonathan

  12. No GravatarJonathan says:

    Are you looking for the amount of views or how long it takes to read the post? You can customize the plugin so that it just puts that, like how you want it by changing the echoin top10.php. Place the code on your page where you want it and you will be good to go.

    Hope that fixes your problem.

  13. No GravatarJonathan says:

    But, if you read the plugin again, you will see what I’m talking about now.

    The plugin is designed for both permalinks and normal links.

    If you wish to use the normal links, p=XX, then use the $p > 0 instead of $id. $id is for permalinks where $p is for the normal link structure. Change this and see what it does for you, that should fix it.

    Read the documentation over again to see how it applies to your specific site.

  14. No GravatarJames Farmer says:

    Hi Jonathan,

    Still having some problems, tried replacing the code with what you put in but it just shut everything down.

    I know the URL is a bit fiunny but it’s working which is the best thing at the moment :o) Might play with it and see what I can do down the line :o)

    BTW - a big feature request would be to limit these to ‘in the last year’ or ‘in the last month’

    Cheers, James

  15. No GravatarOrganic Shadows v.4 says:

    […] s handy, but do you think I can get it running? No way. I grabbed the plugin version from BleakGeek and then successfully added a new table […]

  16. No GravatarJonathan says:

    I will look into releasing a new version in the near future, but I’m not making any promises about it. I’m always open to suggestions and requests.

    Also, I would love to see people using this plugin, so feel free to post your site on here to share your success with it. Thanks.

  17. No Gravatarkaysha says:

    hey there…
    For some strange reason, my top ten doesn’t link to individual posts, but to the same page it’s on…
    Any idea?

  18. No GravatarJonathan says:

    Make sure you have ready through the documentation and also make sure you have included the code correctly inside the loop. If the code is not placed correctly on your page, the loop will not work and it will not count them as neccessary.

  19. No GravatarJonathan says:

    You are using permalinks, so make that you use the permalink echo code instead of the single p= post code.

  20. No GravatarMichael says:

    I’m getting a MySQL error from your plug-in:

    WordPress database 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 1]
    select postnumber, cntaccess from mostAccessed WHERE postnumber =

    Any idea why? Thx

  21. No GravatarJonathan says:

    Two ideas come to mind.

    1) you include the ‘use database’ part of it instead of just the create table

    2) you didn’t copy it exactly.

    I know the code works, everyone else has got it working and I have it working using that. Check your code again and make sure you are using the correct code from the plugin. Thirdly I guess, make sure the table doesn’t already exist.

  22. No GravatarMichael says:

    I did the use database part, I copied it exactly (actually drop’d the table a couple times and started from scratch), and the table exists - I checked.

    Lastly, I know. That’s what is so frustrating. LOL. I’ll take another hack at it. The odd thing is that I used the offending line, pasting it into the command line and got back a result. But the error was still present on the site. Odd.

  23. No GravatarJonathan says:

    Another thing to check is the version of MySQL you are running on your host. I run 4.0.22 and it works fine.

  24. No GravatarMichael says:

    Got it now. Thanks.

  25. No GravatarWeblog Tools Collection » Top 10 Posts/Views says:

    […] tion in the script itself at the top and you may refer to this page I have created to show some examples of the code.   […]

  26. No GravatarPariveshaNa » Number of views would be counted says:

    […] dded a plugin that counts the number of views of each posts from now on The plugin is the Wordpress top 10/view counts script. The script isn’t totally f […]

  27. No Gravatarflora says:

    Hi, Jonathan, Thanks for the wonderful plugin. Is there a code to show up the total visited times? (I am making an internal blog, here is the pictures) http://www.flickr.com/photos/prattflora/18156571/

  28. No GravatarJonathan says:

    Sure, that’s what the plugin is made for. Simply follow the instructions in this post and in the plugin itself and it will help walk you through how to set it up.

  29. No GravatarJonathan says:

    Oh, after rereading it I understand what you are trying to say. No, there is no function for that in this plugin. You can find a simple stats tracker via hotscripts.com or some other website to do that kind of tracking, this plugin is purely on a per post basis. Sorry for the confusion.

  30. No GravatarH.Vogelgesang says:

    Hy, i added this PlugIn into my Blog. But i’m sure that the script count to much… there wasn’t no visitors at my site bit the script still count up.
    does someone has an idea what could be wrong?
    i think the problem is, that the script count search engines, too.

  31. No GravatarJonathan says:

    You are correct. It does count search engine hits as well as human hits. It counts ALL views of the post, no matter what it is. It’s the way the plugin was designed.

  32. No GravatarH.Vogelgesang says:

    Thanks Jonathan,
    do you have an idea which code i have to add that search engines wouldn’t be count?

  33. No GravatarJonathan says:

    Nope, not a clue on that. I didn’t write the plugin, I purely ported it over from misc hacks. You can ask the author, Laughing Lizard.

  34. No GravatarRon says:

    Hi Jonathan,

    I have installed the plugin and added the necessary info to my pages, but I find that the plugin ONLY counts hits on the index page, and not on individual post pages. This also lends to the posts all having the exact number of visits listed. Do you have any idea where the problem might be?
    I am using the connections theme.
    Here is what I have done:

    In post.php:
    …Posted by under  ID, $before=”(”,$after=” views)”); ?> at …

    In index.php:


    no permalinks:
    0) { add_count($p);}?>

    with permalinks:
    0) { add_count($id);}?>

    Any help would be greatly appreciated!

  35. No GravatarRon says:

    So, as I suspected the post got butchered. If you need more info on my code, please email me. I can send you any/all code to diagnose. Thanks again.

  36. No GravatarRon says:

    Why is it that as soon as I post a question to something that has been troubling me for days, I find the *simple* answer within seconds after. I guess it may benefit some other newbie out there to say that if you want the page counted, you need to include the code on that page to count it, not just display it. (I forgot to put the add_count line in the post template, and only had it in index.php. Thanks for the great site and general response to questions. It gave me the encouragement to hang on for that extra three minutes while my brain began to work.

  37. No GravatarRon says:

    What do I need to do to allow users to have a “followup comments via email” option in their posts? I really like this option.

  38. No GravatarJonathan says:

    Well, I’m glad you have it working now. I setup this alternate post so that the users could dialog with each other and I could help them figure out the plugin. I hope you enjoy the plugin :)

  39. No GravatarJonathan says:

    Ah, that plugin. Sorry, for some stupid reason comment #518 was caught up in my spam filter. Not sure why, but I cleared it and it posted.

    The plugin is called ‘Subscribe to Comments’. It was originally created by scriptygoddess but was taken over by MCincubus because she didn’t have time to continue the development of it. Click here to get it

  40. No GravatarSylvain says:

    Hi, Jonathan.

    Cool plugin, works great for me.
    You’re on my “Acknowledgements” roll, thanx.

    Cheers

  41. No GravatarSylvain says:

    Oops! Just realized I got the same problem as Kaysha (#354). Actually, I couldn’t find the line , so I put the line 0) { add_count($id);} ?> above this one: I’m using a highly customized version of Kubrick’s WordPress Default Theme.

  42. No GravatarSylvain says:

    Oops! Just realized I got the same problem as Kaysha (#354). Actually, I couldn’t find the line ##, so I put the line # 0) { add_count($id);} ?>#above this one: ##
    I’m using a highly customized version of Kubrick’s WordPress Default Theme.

  43. No GravatarJonathan says:

    Glad I could umm help :D Thanks for the blogroll too, I really appreciate! I hope you enjoy :)

  44. No GravatarJonathan says:

    Sylvain, glad you got it all fixed. Yeah, it does come hard when every theme is different and there’s only so much documentation you can put into one plugin.

    To answer your question, there is no way as of right now to block your own IP. I was hoping to do that for mine, but I haven’t spent the time to sit down and figure out the place to put it yet, sorry.

  45. No GravatarJaypee says:

    im using the theme Falling Dreams and i’m having little difficulty in distinguishing which part of the code that you provided should i paste into my pages.

  46. No GravatarRob Smith says:

    I downloaded the top10.zip and started to follow the instructions.
    I created the new database table succussfully.
    However, there a number of index.php in the wordpress directories and none contain the code described in thesecond step for the install!
    Where next?

  47. No GravatarJonathan says:

    The index.php that you will be editing is under wp-content/themes/

  48. No GravatarRob Smith says:

    I have three themes installed, “classic”, “default” and “Wuhan”. I’m using Wuhan on the blog. However, if I search all three index.php files, I don’t find the code that the installation instructions say to look for.

  49. No GravatarCarlos el hormigo says:

    Hi Jonathan. I am using this plugin since some days ago and it really works fine. Thank you.
    I only have one stupid problem: the count for each post in the “show pop posts” list is always 1 number higher than the one in “show post count”.
    I mean, for example, if the post X has the number 250 under it, it says 251 in the top list. I don’t know almost anything about php, so no idea about how to solve it. Can you help me?
    Thanks again, greetings from Spain, and sorry for my English.

  50. No GravatarJonathan says:

    Hi Carlos. Yes, that is a known bug. I’m sorry, but I only ported it over and I’m actually hoping someone will rewrite the plugin because seems to be pretty sloppy. It gets the job done, but it could be written so much better. When a new plugin is released I will post about it, but for the time being you will just have to live with it, sorry.

  51. No Gravatarimbuez » Wordpress Top10/View Count Plugin says:

    […] http://www.bleakgeek.com/2005/04/08/wordpress-top10view-count-plugin […]

  52. No GravatarHui’s weblog » Blog Archive » WordPress: read count for each post says:

    […] Just added in a “Read Count” plugin for my WordPress blog. The plugin is done by Jonathan (http://www.bleakgeek.com/2005/04/08/wordpress-top10view-count-plugin) […]

  53. No GravatarCottonIJoe.de » Projektseite says:

    […] Zunächst vielleicht ein schon vorhandenes, aber überarbeitungsbedürftiges Wordpress-Plugin, und danach…. mal sehen projekte, programmieren, wordpress, plugin, code […]

  54. No Gravatardamn.be » Waiting to upgrade to WP2 says:

    […] Top10 […]

  55. No GravatarAnushaRaji » links for 2006-01-22 says:

    […] BleakGeek.com » Wordpress Top10/View Count Plugin Wordpress Top10/View Count Plugin (tags: wordpress plugin lazysheep) […]

  56. No GravatarThe Humanaught says:

    Hey Johnathan, thanks for making this post available for us all to find some help installing this cool plugin… one thing:

    And this answers a comment a couple up… many of the themes don’t contain the “find this line” line from the plugin’s install instructions.
    Basically, if you want the counter to count a “hit” everytime your main page loads (index.php), then put the tag in the Main Index Template (index.php) using the Theme Editor.
    However, and what I believe is a much more practical solution, and much more true to the “top 10″ idea is to put the code in the Single Post (single.php) page, as this will only add a “hit” to the count if the page is directly accessed.

    Whatever you choose, you should put the tag right after this line:
    [?php while (have_posts()) : the_post(); ?]
    (changing the [ to

  57. No GravatarJonathan says:

    Honestly, this install is pretty old. I believe that Mark released an updated version because people were wanting one. You should check out his site again for the updated version, this goes for everyone.

  58. No GravatarThor Jarle says:

    Hi, I liked your plugin and I got it to work right away, but I dont like that it counts and shows posts that I havent published yet (I want to work on them for a long time and then publish them, and then they have gotten alot of “hits” allready). Do you know how to hinder it to count when it isnt published?

  59. No GravatarBrandon says:

    Great code! Is there any way put the date of the post next to the link, rather than the number of views? I tried plugging in the same code from the post page, but it didn’t seem to like that.

    Thanks!

  60. No GravatarJenda says:

    Is there any way to not count your own visit to the page?

  61. No GravatarJonathan says:

    I am not in chrage of this plugin anymore, sorry.