Plugin: Stats Helper Functions And Widgets
What it does? It helps you retrieve data from wordpress.com stats and put it on your blog.
Download:
http://wordpress.org/extend/plugins/wordpresscom-stats-helper/
So far it provides helper functions and widgets to display:
- Most Visited Posts – lists the most visited posts along with a link to the post and the views count
- (New, Recommended) Function usage – put it into your theme template:
if (function_exists('wpcomstats_most_visited')) { wpcomstats_most_visited($options); }Where:
$options – An array which can contain the following key – value pairs:- posts, before, after, show_count, days – Same as before, see bellow
- show – What type of items to display; one of ‘posts’, ‘pages’ or ‘both’
- (Old) Function usage – put it into your theme template:
if (function_exists('wpcomstats_most_visited_posts')) { wpcomstats_most_visited_posts($posts, $before, $after, $show_count, $days); }Where:
$posts – Number of posts to be fetched
$before, $after – HTML/text to put before and after each post
$show_count – Set to false if you don’t want to display post views count
$days – Number of days for which the stats to be calculated. Valid values: 0 (all), 1 (today), 7 (this week), 30 (this month – I know, I’m lazy but gotta KISS) - Widget – just drag and drop it on a sidebar.
You can configure it’s title, the number of posts that you want to list, if to show views count for each post, for what time frame to calculate stats and if you want to restrict the lists to posts or pages only.
- (New, Recommended) Function usage – put it into your theme template:
- Blog Visits – shows a counter of blog visits since stats where enabled
- Function – put in theme template:
if (function_exists('wpcomstats_visits')) { wpcomstats_visits($before, $after, $post_id, $days); }Where:
$before, $after – HTML/text to put before and after views count
$post_id – Post/page id for which the stats must be calculated (use null for all blog stats)
$days – Number of days for which the stats to be calculated. Valid values: 0 (all), 1 (today), 7 (this week), 30 (this month) - Widget – drag&drop to sidebar.
Title is configurable, so is the timeframe for which the stats is to be calculated. The widget automagically detects single post/page contexts and shows stats for that post/page only.
- Function – put in theme template:
Note:
- The plugin assumes you have installed the wordpress.com stats plugin and configured it properly. If not, it will display some error messages. Pretty self descriptive.
- The plugin caches the data from wordpress.com stats so it won’t put too much load on their system. This means visits to posts will probably not be updated in real time
More to come… soon! Like it, hate it?
Changes:
0.5
- Added possibility to filter most visited items by type (post/page)
- Added a new function for most visited items, with more flexible arguments passing
- Safer fetching from stats.wordpress.com (try… catch)
0.4
- Added possibility to restrict the timeframe for stats
- Added possibility to get views count for a specific post/page (by id)
0.3:
- Added a function and a widget for blog visits.
0.2:
- Implemented a custom, option-based, cache for stats fetch data (on Andy’s suggestion)
- Capped the maximum post count to 25 (you won’t be using this to spam, right?)
- Added a configurable option for views count display
103 Comments »


March 7th, 2008 at 1:57 pm
Nice, I’ll use it if I can show total visits with it
March 7th, 2008 at 2:07 pm
Thanks for the suggestion, I’ll put it in!
BTW: I loved the Crocus pic you took a couple of days ago: http://www.flickr.com/photos/panthera_be/2312215656/
March 8th, 2008 at 1:08 am
thx
March 28th, 2008 at 2:14 pm
Hi, your plugin does exactly what I was looking for
… I spent a lot of time looking for this… maybe searching wrong terms but at the end I found it! 
But non listed in wp.org/extend why?
Anyway, a question: I will be happy to keep reference links to this page on my blogs, but I’d like to move them from the bottom of the widgets maybe in the footer… am I allowed to do so? This will make me save much time in tuning CSS.
Please forgive my poor english and thanks again for this plugin.
March 28th, 2008 at 5:57 pm
@Johnnus: Feel free to use the plugin as you wish (including any modifications)! A link back to my page is not required (unless you’re re-distributing the modified plugin) but I would very much appreciate it.
I have applied for wp.org/extend hosting for almost a month but I guess they have a lot of requests and it takes time to activate mine.
March 28th, 2008 at 6:33 pm
Thank you very much Vlad!
I hope to see your plugin soon on wp.org because it deserve to be there.
Thanks again
April 2nd, 2008 at 2:40 pm
This looks to be exactly what I wanted. Is it possible that I could display each posts viewcount in single.php?
April 2nd, 2008 at 2:50 pm
Not at the moment Tim, but I’ve put this in my TODO list and it’ll be soon available.
April 2nd, 2008 at 4:14 pm
That’s awesome, thanks!
April 7th, 2008 at 4:32 pm
oh-ho! 0.4! … and download from wp.org!!! Great!
Good job Vlad!
April 15th, 2008 at 9:03 pm
Would be sweet if this could show top posts by category ?
April 17th, 2008 at 6:18 pm
hey Vlad
This plugin is just what I’m looking for – however excluding pages is a must for me as my most popular posts are all pages!
Where would I put this conditional tag to exclude pages from the output?
Thanks again for your hard work.
April 18th, 2008 at 10:10 am
Hi Alex,
You’d have to get down&dirty and modify the source, instead of:
$most_visited_posts .= $before.'<a href="'.$url.'" title="'. $name.'" rel="nofollow">'.$short_name.'</a>'. ($show_count?'('.$count.')':'').$after;you’ll have to put:
$id = $cols[0]; if (!is_page($id)) { $most_visited_posts .= $before.'<a href="'.$url.'" title="'. $name.'" rel="nofollow">'.$short_name.'</a>'. ($show_count?'('.$count.')':'').$after; }@Kes: noted!
April 18th, 2008 at 1:12 pm
Vlad
Thanks for your response – however the code you gave seems not to work for me. It just doesn’t output anything on the site.
I have
on the sidebar – when I use the plugin normally it outputs the 5 most popular posts from the week but changing the source seems to output nothing. (there’s no error messages either)
Thanks for your time!
April 18th, 2008 at 1:40 pm
Alex, this was a quick hack, if all your top 5 most visited were pages then none gets displayed. Try to increase the number of posts (as pages will be removed).
April 18th, 2008 at 2:11 pm
I do now have some posts in my top 5 so I’m not sure it’s that – to make sure I did try making it ‘20′ and the output was still blank.
April 18th, 2008 at 2:29 pm
Can’t really tell what’s happening, but I’ll try to release a new version soon (1-2 weeks at most) and it will cover this feature too. Stay tuned!
May 3rd, 2008 at 4:33 pm
Updated the plugin to version 0.5 which includes filtering by type (post/page). Filtering by category didn’t make it into this version as I’m not very sure which would be the best approach yet, I’m still open for suggestion on how that would work.
May 3rd, 2008 at 10:50 pm
I love the plugin. However, when i upgraded to 0.5 i was unable to do so due to this error:
Parse error: syntax error, unexpected ‘{‘ in /home/monarchh/public_html/wordpress/wp-content/plugins/wordpresscom-stats-helper/wpcomstats-helper.php on line 156
Any suggestions?
May 3rd, 2008 at 10:58 pm
Ooops! Looks like I let myself carried a bit too far, try… catch works only in PHP5. I’ll figure something out soon, thanks for dropping the note!
May 3rd, 2008 at 11:17 pm
I upgraded to PHP5 just before upgrading the plugin.
May 5th, 2008 at 5:15 am
I don’t use PHP5.
May 6th, 2008 at 6:22 pm
Ok then, I updated the plugin to 0.5.2 which is PHP4-safe again!
May 6th, 2008 at 6:26 pm
Hi Vlad
I’m getting this error now when I try and activate the new version of the plugin…
Parse error: parse error, unexpected '&', expecting T_VARIABLE or '$' in /hsphere/local/home/huckftp/huckmagazine.com/wordpress/wp-content/plugins/wordpresscom-stats-helper/wpcomstats-helper.php on line 212May 6th, 2008 at 9:20 pm
Hi, I am getting this error:
Parse error: syntax error, unexpected ‘&’, expecting T_VARIABLE or ‘$’ in /…../wp-content/plugins/wpcomstats-helper.php on line 212
Thanks! Would love to use the plugin.
May 6th, 2008 at 9:21 pm
Same here.
May 6th, 2008 at 11:00 pm
Thanks for the feedback, it seems there was some other stuff there that’s only supported in PHP5 (passing by reference in foreach). I fixed that too and updated to v1.5.3. Sorry for the mess!
May 7th, 2008 at 1:55 am
Thanks! Sorry for nagging you back to php4.
May 7th, 2008 at 2:06 am
Vlad, can you help me out? I’m not sure where to specify posts only in this:
wpcomstats_most_visited_posts(‘10′,”,”,’true’,'7′);
May 7th, 2008 at 3:24 am
OK, all I need help in is removing pages from the stats…. But I am using your plugin for:
The visit total (all-time) seen in the footer and the statistics page:
http://timwheatley.org/statistics/
The view count (all-time) seen in the left menu of each post with the text “It has been viewed x times.” like you can see here:
http://timwheatley.org/2008/05/05/panoramic-view-of-chicago-skyline-at-dawn/
I just had to insert the in-loop post id call into your code to bring the total for the post being viewed.
Everything on the stats page (linked above).
This is an awesome plugin Vlad, thanks so much!
May 7th, 2008 at 7:20 am
Tim, an example on how to display only posts is in this announcement.
May 7th, 2008 at 9:59 am
I have multiple blogs (2 actually) – can I combine the hits on both blogs to provide a combined count – one was a wordpress.com blog which still exists but has been superceeded by a selfhosted one. I can use the wordpress.com stats plugins to see the stats on both blogs but want to use your plugin tgo add the wordpres.com stats to the new one to give a TOTAL count – nice plugin BTW !!!
May 7th, 2008 at 2:31 pm
Thanks Vlad. That’s actually something new I learned.
I have been editing PHP since 2001 and only now do I know the name of an array.
When people said ‘array’ in the past I never knew what they meant, but I’ve even used them in my own code.
That’s what you get when you learn PHP by editing existing script I guess.
May 7th, 2008 at 2:57 pm
Ok, I think I am done. Very happy!
I edited the script to cut the post title length at 70 letters rather than 40, but other than that I didn’t need to change anything. Thanks again Vlad.
May 8th, 2008 at 6:48 pm
Vlad, nice plugin. I was using version 0.4 without any problems. After I upgraded automatically to 0.5.3, with WordPress 2.5.1, the top posts list showed only an odd list of single digits or numbers, not the post titles, so I reverted back to version 0.4. I tried removing the widget and reinstalling it, no luck. So, I’ve restored 0.4 and will wait for another future version.
May 8th, 2008 at 9:38 pm
Sorry to hear that! Could you tell me more details about the settings you’re using on the widget to try and reproduce your results?
Also, instead of removing/installing the widget, I think disabling and re-enabling the plugin would have helped more.
June 5th, 2008 at 8:30 pm
I love the plugin.
However, our most viewed post only comes up with the number of visits not the name of the post.
Any thoughts on how to fix this?
July 15th, 2008 at 10:36 am
Hi,
liked this tool immensely.
Today, suddenly I got a problem.
“Most Popular Visits” widget is working fine but “Blog Visits” widget is causing a problem.
For all the pages/posts it is showing a single number – ‘6′.
Please let me know what to do to fix it.
You can check out the left side bar in http://ayaskantam.com to find this scenario.
Regards,
Ashok Varma
July 15th, 2008 at 8:32 pm
Ashok, are you running wp 2.6 by any chance? I just tested the new version and found some incompatibilities which I will fix really soon.
July 24th, 2008 at 1:01 pm
This is a great plug in
July 28th, 2008 at 4:32 pm
Hi,
since the upgrade to WP 2.6, all data in the Most Visited Posts sidebar widget is blank. Yes, I am seeing stats from the current wordpress.com 1.3.2 stats plugin, but the Most Visited Posts plugin isn’t showing anything. I’ve tried deactivating and reactivating. Any ideas what to do?
July 30th, 2008 at 6:36 am
Nice plugin. Is it possible to show total visit per day and total view for all time at the same time?
July 30th, 2008 at 11:29 pm
hi, vlad. great plugin. I used it also several weeks and after update of stats 1.3.2 I have the problem that it always tells me “.no data yet!”.
July 31st, 2008 at 4:52 pm
going back to stats 1.2.2 and stats-helper works fine again.
August 16th, 2008 at 10:31 pm
So stats 1.3 with this plug in wont work right? It seems to work fine if I select posts and pages, but if I just select posts I get nothing returned. Since pages are hit so much more often on our site, it doesnt help a lot since they take up the first 10 to 20 spots regularly.
September 13th, 2008 at 3:01 am
I have two blogs.I am using the same API key for the STATS program.
The first one works fine and the widget shows the visits.
The second has had very few visitors but the widget says it does not have any data.
Any ideas?
September 15th, 2008 at 10:08 am
Richard, can you post the link to that blog? I’d like to take a look!
September 15th, 2008 at 11:57 am
is it possible to show the pageviews on the widget and not jsut the unique visitors?
September 15th, 2008 at 3:23 pm
This is the link:
http://www.endinghomelessnessinnj.org/
September 15th, 2008 at 4:07 pm
Richard: you could check the value yourself (replace api_key with correct one, I filled the correct blog_id for you since that’s public): http://stats.wordpress.com/csv.php?api_key=api_key&blog_id=4760579&table=views&summarize=true&days=-1
Phat_Trance: wordpress.com stats offer info about page views, that’s what the plugin shows.
September 15th, 2008 at 4:47 pm
OK
The key is correct in blog stats as it works there.
I am a little confused how to do what you are suggesting?
September 15th, 2008 at 4:53 pm
I was suggesting to check what stats.wordpress.com is reporting directly (using the given link). Then we know if it’s a problem with the plugin or the stats service.
September 15th, 2008 at 5:02 pm
OK.
I will try to do that.
Stats is workign as I can see numbers on the dashboard. The only place i cannnot see them is the plugin.
September 17th, 2008 at 9:31 pm
on occassion i do get stats and then they disappear. As it is a new site I only have 41 hits. Is that an issue?
Can you check out the site?
http://www.njaneh.org
September 27th, 2008 at 3:16 am
Hi Vlad
Thanks for the plugin – question: Is it possiblt to edit ‘blog visits’ field so that only blog visits after aug 31 2008 are counted?
thanks
September 29th, 2008 at 9:16 pm
Hi Vlad, your plug in was just what I was looking for. But I can’t get it to only show posts. If I set the widget that way, nothing is shown except the widget title. I’ve tried to change around some code as you’ve written earlier in this thread, but no success. Any ideas?
September 30th, 2008 at 7:42 am
If you’ll forgive my presumption to modify your code, I made some improvements after realising your plugin wasn’t working on my WP 2.6.2 site when selecting to show posts only. The plugin was just displaying no results at all, because it was using an empty string for the ‘include’ part of the post query, instead of ‘any’, which it should’ve been.
I also made some improvements to the interface, adding defaults, replacing a drop-down used for a boolean value with a checkbox, and adding the option to truncate titles or not, and the option to display or not display the link to the plugin.
I’ve temporarily uploaded a zip file containing the new version as well as a patch showing the changes to http://michael.tyson.id.au/wpcomstats-helper-0.5.5.zip . Tell me when you’ve downloaded it and I shall remove it.
Let me know what you think.
September 30th, 2008 at 11:10 am
Thanks Michael!
I downloaded the file and I’ll have a look at the code as soon as possible.
Any contributions to the plugin are always welcome!
October 5th, 2008 at 3:31 pm
hi. Thanks for plugin
perfect.
Regards
October 8th, 2008 at 4:52 pm
Any word on a fix for Wordpress version 2.6.2? Ever since the upgrade my most viewed posts have stopped reporting – it just shows nothing.
I’ve upgraded my code to the newest recommended array format but no change.
October 8th, 2008 at 5:20 pm
You are using the latest version of the plugin, right? Please also try a disable/enable cycle for the plugin (clears the cache)!
October 11th, 2008 at 2:59 pm
same here.
Ever since i updated the plugin i have not got it to work.
Disable/Enable were done but no help.
The code to call the script was changed to the array one.
No list at the moment.
October 11th, 2008 at 3:28 pm
Yuval, your problem might be solved if you make sure your $options['show'] has one of the ‘post’, ‘page’ or ‘both’ values set. There’s a missing default there that’s been causing people a headache. Let me know how this works out!
October 12th, 2008 at 12:30 pm
I’ve got the same problem, the “Most Read Posts” list isn’t working any more. Buggy release?
October 12th, 2008 at 12:46 pm
Thanks, that worked!
October 15th, 2008 at 11:48 pm
Hi Vlad,
Same case here. The plugin isn’t working since last update.
October 16th, 2008 at 11:25 am
I added a default for $options['show'] – please see version 0.5.5.2
October 20th, 2008 at 9:28 pm
I switched from wordpress.com to hosting through bluehost. I downloaded your plugin but it only gives me stats after I switched to the new hosting service. Is there a way to combine both stats? I used the same API code but the WP.com stats table gives me a choice to select between these two sites but not one that combines all stats.
Thanks.
October 21st, 2008 at 11:24 am
Stats are recorded separately for different URLs. Since I supposed you’ve changed the URL while moving from wp.com to your own domain and hosting, there’s nothing you can do about it (unless you can convince the wp.com stats team to move your old stats to your new URL).
October 21st, 2008 at 6:33 pm
Yes, that makes sense. Thank you very much!
October 22nd, 2008 at 5:55 am
Vlad, I’m following your installation, but I can’t seem to get blog visits working… most visited works just fine.
I’ve installed the wordpress stats. Any ideas? Thanks
October 22nd, 2008 at 9:34 am
It’s not working as in: it doesn’t show anything or it shows wrong data/error? Can you send me a link to your site so I can check out the behaviour?
October 22nd, 2008 at 11:06 am
it shows that ‘There’s No Data Yet’. I already had many visits so, this is surely wrong. Thanks for the help. I really appreciate it. This is such a good plugin.
http://howtogeta40.clicknsmart.com
November 1st, 2008 at 5:18 pm
Still no luck, the newer version continue to hide the “Most Visited Posts” thingy…
Reverting to 0.5.4 once again….
November 3rd, 2008 at 4:16 pm
Hey there, I’ve the same problem like IndoS. It shows: “There’s no data yet.”
The wordpres.com/stats plugin is up and running for over 2 months now and there are some visits.
At the moment I’ve got 2 blogs running on 1 API. Mayby that is the problem??
November 8th, 2008 at 8:21 am
I’ve installed the plugin and added the “most visited post” widget.
on my page you can see “Top Post”, but no post or links are showing up.
Do I need to add this code and if so, where exactly does this code need to go?
if (function_exists(‘wpcomstats_most_visited’)) {
wpcomstats_most_visited($options);
}
November 10th, 2008 at 6:29 am
I have installed wordpress stats plugin and wordpress stats helper plugin. Both of them are installed properly and I can see stats my dashboard. Now I want display most visited post in tab . So I am using this following code
wpcomstats_most_visited($options = array (
‘posts’ => 10,
‘before’ => ”,
‘after’ => ”,
’show_count’ => false,
‘days’ => 30,
’show’ => posts
));
but nothing is displayed. Can you please help me this
November 10th, 2008 at 7:46 pm
Nevermind my question as it seems to be working now.
November 13th, 2008 at 10:11 am
I’ve had the Stats Helper plugin installed for a few months at the bottom of my blog to display a counter of visits/hits. It was working fine until recently when an error appeared…
Warning: Invalid argument supplied for foreach() in /wp-content/plugins/wpcomstats-helper.php on line 107
However it still displays the hits/view count after the error. I’ve tried to tinker a few things but nothing seemed to help. Any ideas?
November 15th, 2008 at 4:58 pm
Thanks for pointing that out. I just made a fix and it should soon be available as version 0.5.5.3
November 15th, 2008 at 6:15 pm
Works great, thank you
December 2nd, 2008 at 1:03 am
Hi Vlad,
Thanks for developing this wonderful plugin.
I wanted to use this plugin to display most viewed post on my website http://www.asianweek.com
I have installed wordpress stats plugin and wordpress stats helper plugin.I can see the stats appearing on my dashboard but however i cannot see the stats on my Most viewed section. here my code. Most Viewed
Please let me know where i am going wrong
Thank you in advance
December 10th, 2008 at 1:54 pm
This is a great plug in
December 11th, 2008 at 4:19 pm
Hey there, just installed your plugin and it’s awesome. Short, sweet & simple.
I’m trying to modify it to display top categories (i.e. count the total views for each category) but I’m struggling. Any tips or suggestions on where/how to start?
Cheers
December 15th, 2008 at 11:24 am
Hi,
Sometimes instead of getting the data that I was supposed to get, it displays the last posts.
Any ideas why that happens? For me it seams to be completely random.
December 18th, 2008 at 5:08 am
My problem may not be solvable.
I upgraded and accidently created a second site with stats. Both have the same name. How can I fix this so that my stats will not only work but reflect my entire history?
December 18th, 2008 at 12:13 pm
Richard, what do you mean by “accidently created a second site with stats”? A new API key or a new blog id?
For all things related to API keys, blog IDs and stats merging I think you should talk to the wp.com support.
December 26th, 2008 at 12:27 am
Tried to update to 0.5.5.3, no luck once again: the plug-in continues to not to show the “most read posts”. Disappointing…
Reverting back to 0.5.4 for the third time….
January 26th, 2009 at 5:25 am
Love the plugin and it was exactly what I wanted.
Would it be possible to filter my categories and have multiple instances in the sidebar?
February 21st, 2009 at 12:52 pm
ma intereseaza sa imi apara doar vizitele totale. Nu am sidebar. Ce cod trebuie sa scriu?
February 21st, 2009 at 1:00 pm
Cristi: to display all visits to your blog you can use:
if (function_exists('wpcomstats_visits')) {
wpcomstats_visits('', '', null, 0);
}
in your template.
February 21st, 2009 at 1:16 pm
And if I get: No data yet! it means it does not display the visits I have previously installing this plugin?
February 21st, 2009 at 3:16 pm
This plugin uses data from the Wordpress.com stats plugin. If you installed that recently, it’s normal to get “No data yet!”.
February 21st, 2009 at 3:32 pm
I have it installed few months ago… in June last year…
February 22nd, 2009 at 9:51 am
Is this plugin support wp 2.7.1?
February 22nd, 2009 at 10:03 am
Yes, it works with wp 2.7.1. Actually, this site is using that version.
February 22nd, 2009 at 5:17 pm
Is there any way to put a txt after the blog visits? And can I remove the link? It doesnt look nice on my site.
February 23rd, 2009 at 1:51 am
This is pretty close to what I need, but are you looking at extending it? In particular, I’m after an interface for the most popular search terms.
March 12th, 2009 at 10:42 am
I am trying to tweak your code, changing the results maximum from 25, to 50. But even when changed, it still only returns 50. Is this a setting locked from wordpress.com? Any ideas?
March 29th, 2009 at 1:40 am
Hello!
Very Interesting post! Thank you for such interesting resource!
PS: Sorry for my bad english, I’v just started to learn this language
See you!
Your, Raiul Baztepo