Wordpress Top10/View Count Plugin
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()) : ?> <h3 id=”post-<?php the_ID(); ?>”>
<?php while (have_posts()) : the_post(); ?>
<?php if ($id > 0) { add_count($id); } ?>
<div class=”entry”>
<?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.




April 16th, 2005 at 1:27pm
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
April 21st, 2005 at 10:47am
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 :)
April 24th, 2005 at 12:06pm
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?
April 24th, 2005 at 12:10pm
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.
April 24th, 2005 at 12:25pm
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.
April 24th, 2005 at 11:23pm
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
April 25th, 2005 at 1:20am
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
April 25th, 2005 at 6:07am
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
April 25th, 2005 at 6:11am
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
April 25th, 2005 at 9:41am
Jonathan: Thanks for your help. Works great now.
April 25th, 2005 at 9:44am
Glad it works. Feel free to drop another note if you need anything else.
Blessings,
Jonathan
April 26th, 2005 at 12:20pm
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.
April 26th, 2005 at 12:29pm
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.
April 26th, 2005 at 5:07pm
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
April 26th, 2005 at 9:27pm
[…] 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 […]
April 27th, 2005 at 1:57pm
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.
April 28th, 2005 at 10:10am
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?
April 28th, 2005 at 10:19am
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.
April 28th, 2005 at 10:21am
You are using permalinks, so make that you use the permalink echo code instead of the single p= post code.
May 4th, 2005 at 7:26pm
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
May 4th, 2005 at 8:22pm
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.
May 4th, 2005 at 8:43pm
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.
May 4th, 2005 at 8:47pm
Another thing to check is the version of MySQL you are running on your host. I run 4.0.22 and it works fine.
May 4th, 2005 at 9:09pm
Got it now. Thanks.
May 13th, 2005 at 8:47am
[…] 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. […]
May 22nd, 2005 at 11:51pm
[…] 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 […]
June 8th, 2005 at 2:55am
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/
June 8th, 2005 at 6:24am
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.
June 8th, 2005 at 6:48am
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.
June 11th, 2005 at 8:03am
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.
June 11th, 2005 at 8:08am
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.
June 11th, 2005 at 8:18am
Thanks Jonathan,
do you have an idea which code i have to add that search engines wouldn’t be count?
June 11th, 2005 at 8:21am
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.
June 22nd, 2005 at 1:54am
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!
June 22nd, 2005 at 1:55am
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.
June 22nd, 2005 at 2:03am
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.
June 22nd, 2005 at 2:58am
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.
June 22nd, 2005 at 6:42am
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 :)
June 22nd, 2005 at 9:19am
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
June 23rd, 2005 at 5:42am
Hi, Jonathan.
Cool plugin, works great for me.
You’re on my “Acknowledgements” roll, thanx.
Cheers
June 23rd, 2005 at 6:00am
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.
June 23rd, 2005 at 6:02am
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.
June 23rd, 2005 at 6:46am
Glad I could umm help :D Thanks for the blogroll too, I really appreciate! I hope you enjoy :)
June 23rd, 2005 at 4:55pm
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.
July 10th, 2005 at 5:07am
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.
July 14th, 2005 at 5:34am
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?
July 14th, 2005 at 6:57am
The index.php that you will be editing is under wp-content/themes/
July 14th, 2005 at 8:39am
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.
July 22nd, 2005 at 3:02am
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.
July 22nd, 2005 at 6:26am
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.
August 26th, 2005 at 1:09am
[…] http://www.bleakgeek.com/2005/04/08/wordpress-top10view-count-plugin […]
October 24th, 2005 at 9:02pm
[…] 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) […]
November 27th, 2005 at 5:00am
[…] Zunächst vielleicht ein schon vorhandenes, aber überarbeitungsbedürftiges Wordpress-Plugin, und danach…. mal sehen projekte, programmieren, wordpress, plugin, code […]
December 28th, 2005 at 1:02am
[…] Top10 […]
January 22nd, 2006 at 3:20pm
[…] BleakGeek.com » Wordpress Top10/View Count Plugin Wordpress Top10/View Count Plugin (tags: wordpress plugin lazysheep) […]
June 3rd, 2006 at 11:43pm
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
June 3rd, 2006 at 11:46pm
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.
June 20th, 2006 at 2:33am
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?
July 27th, 2006 at 7:11am
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!
September 10th, 2006 at 3:45pm
Is there any way to not count your own visit to the page?
September 10th, 2006 at 3:46pm
I am not in chrage of this plugin anymore, sorry.