31 Responses

  1. Simon
    Simon Published |

    Haven’t managed to get it working yet, but thanks a lot for this very valuable tut (+ files)!

    Reply
  2. Stephen Cronin
    Stephen Cronin Published |

    Hi Justin,

    Interested to know why you didn’t just recommend that people use use an established plugin such as WP-PostViews by Lester Chan – is there any particular reason you didn’t?

    It’s probably worth pointing out to people what happens if they use a caching plugin, as many people do (including this site I see from the source).

    Because you’re counting the views via JavaScript, it should count them correctly (except for W3 Total Cache if the Database cache is turned on). Having said that, I’ve had problems with nonces and caching plugins, so it may not count them after all with the nonce in place.

    When displaying the count (entry_views_get), if the page is cached, you won’t see the current count, you’ll see the count at the time the page was cached. Shameless plug: I’ve written a WordPress plugin called Ajax_the_views which uses Ajax to display the count for WP-PostViews. If this issue is important for anyone, I’m sure they could adapt it to work with the Entry Views script.

    Anyway, thanks for contributing this script.

    Reply
  3. Jan Egbert
    Jan Egbert Published |

    Brilliant! I love deactivating plugins because my theme can do the coolness.

    Reply
  4. Simon
    Simon Published |

    Hmm unfortunately I’m not able to get it working yet. I don’t think I have any custom post types (just taxonomies?). Following on from what you said above, I’d have to add “post” to make it work on post, is that right?

    The file definitely gets loaded and I added the php snippet unchanged to my single php file (withing the loop). Sorry for being a bit slow with adoption here.

    Reply
  5. Matt
    Matt Published |

    You can also get this info from the WP.com Stats plugin API pretty efficiently, without causing any extra writes to your DB. Shoot me an email if you need any pointers.

    Reply
  6. Stephen Cronin
    Stephen Cronin Published |

    Hi Justin,

    I’ve been trying News out on one of my sites when I came across a ‘problem’ I traced back to the views functionality. It’s not really a ‘problem’ and won’t occur for most people, only people like a bit of extra security like me!

    The views functionality seems to send information to wp-admin/admin-ajax.php. I password protect my wp-admin directory, so visitors to my site get a pop up dialog box asking them to enter the password for wp-admin on each page load.

    As I said, it won’t happen for most people, but it will happen for some.

    In my own plugins I steer away from using anything in the wp-admin folder on the client side. It’s not that hard to write your own server side script to deal with Ajax requests – although I’ve found you can’t use nonces because caching plugins won’t work with them.

    Anyway, I’ve just disabled the theme support for entry-views so I’m good – I just thought I’d let you know.

    Reply
  7. Weergaven tellen zonder WordPress-plugins en Analytics
  8. Philip
    Philip Published |

    hi Justin,

    first of all i would like to thank you very much for this article and code sharing,
    i would like to use your code in my theme but i can’t make it work with the template tag you suggest,
    the only way to get the number of views in a post is by using the shortcode.

    so, now i will use it in a template like this:
    ‘ echo do_shortcode(‘[entry-views after=" Views"]‘); ‘

    if you know a better way to do this i will really appreciate it,
    thanks a lot!!!

    Philip

    Reply
    1. Sami Keijonen
      Sami Keijonen Published |

      Thanks Philip!

      I was wondering why I can’t get the number of views working, but this

      ‘ echo do_shortcode(‘[entry-views after=" Views"]‘); ‘

      works in my loops also.

      Reply
  9. Frankie Jarrett
    Frankie Jarrett Published |

    Justin,
    I was able to whip up a simple Dashboard Widget for displaying the Top 10 Posts with the help of your extension. I plan to customize it even further for use with Custom Post Types. This had added more value to my theme and future themes, thanks bro! :)

    Top 10 Posts Dashboard Widget
    http://www.pastie.org/1396682

    Reply
  10. Adam Capriola
    Adam Capriola Published |

    I love keeping post views… is there any way to sync the post view count of this with Lester Chan’s plugin? Or maybe sync is the wrong word… just catch it up to speed when you first install it?

    I’d consider using this instead of WP PostViews if it’s lighter, but I’ve already got post views racked up and starting from ground zero probably won’t fly.

    Actually maybe this would work:

    WP-PostViews saves the count in the meta key “views”. This saves it in “Views”. So if I change Views to views in your plugin, then disable WP-PostViews, it should be a flawless transition… right? :)

    Reply
    1. Frankie Jarrett
      Frankie Jarrett Published |

      sounds right to me…of course as long as you then disable the old plugin

      did it work?

      //Frankie

      Reply
  11. Panagiotis
    Panagiotis Published |

    Thanks for sharing the code. I managed to add the widget from News theme to WooThemes Estate theme. However it will be very useful if we have the ability to choose a timeframe, for example popular posts during the latest 7 days.

    Reply
  12. Theme Digital
    Theme Digital Published |

    Hey guys,

    What would the WP community be without people like you? Thanks so much!

    Here’s how I’m using your wonderful extension: http://www.themedigital.com/popular-wordpress-themes/

    I’m thinking of creating extra page templates for viewing popular posts (themes) by month, week, day… it currently (obviously) show all time popular posts.

    Thanks again… for being!

    Reply
  13. Taeke
    Taeke Published |

    handy little script.. Would it be possible to count +1 for all articles on an archive page?
    I would like to count all views of titles, no matter if people click and read the single post.. Like attaching the script to the title or so. How to go about?

    Reply
    1. Taeke
      Taeke Published |

      Got it working.. extended the file a bit

      Reply
  14. ovidiu
    ovidiu Published |

    I would like to use this on a photo blogging theme, where you see 1 last post with full size image straight on the home page so there is no need to click the post title. visitors are simply paging through the homepage’s pages to see older pics.
    it rarely happens that someone clicks on an actual post.

    could this script track this kind of views too?

    Reply
  15. DRAE
    DRAE Published |

    Can you post a screen shot so we can see how the end results are supposed to look?

    Reply
  16. Jeff
    Jeff Published |

    Thanks for the script! I do have one mysterious problem though. The script works like a charm locally, but when I uploaded the blog to the company website it does not print the post links. There does not seem to be an error. It simply does not get/display the links.

    Any thoughts?

    Jeff

    Reply
  17. Hayden
    Hayden Published |

    If you want to print a line on your post entry like “Viewed x times” or in the case of 1 view, “Viewed 1 time”, then you will need to make a comparison using a pure numeric version of the views meta data to determine whether to use “view” or “views”.

    The function entry_views_get() returns a number formatted with the Wordpress number_format_i18n() function which will format the number according to your localization, which may not be an integer and will cause problems for your comparison. In my case it adds a comma to numbers larger than 999.

    You will either need to get the views as a number from meta data field directly, modify the function to out put singular or plural versions of words, or use a regular expression to remove the comma from the value returned by entry_views_get().

    Reply
  18. Shah Hussain
    Shah Hussain Published |

    Welldone, I have no words, how to thank you…. man

    Advice to everybody.. dont waste time on using plugins use this Justin script, this is the best and easy to manage..

    Thumbs Up…!!!

    Thanks Alot Good Bye

    Reply
  19. GabiPopa
    GabiPopa Published |

    Hey, Justin.

    I cannot make it work. Could you please send me an email and explain like for really stupid people exactly what to do?….and I mean step-by-step instructions. I tried it, but all I could do in the end is upload the entry-views.php in the theme folder. I would really appreciate it if you could spare some time and help me with this.

    I want to display weekly popular posts, monthly popular posts and all time popular posts (and to display the number of views on every post). Can you help?

    Reply
  20. ken
    ken Published |

    Looks like your template tag is missing a bit of code? There needs to be an “echo” to display the view count.

     'Views: ' ) ); ?>

    should be:

     'Views: ' ) ); ?>
    Reply
  21. john
    john Published |

    Hi Justin,

    I’ve added the extension to my theme and I’m getting an warning on my pages and it won’t display the post count

    Warning: Creating default object from empty value in /home/grizzable/public_html/wp-content/themes/hsp/extensions/entry-views.php on line 61

    It is referring to the entery-views.php file on line 61 where it says

    /* Set the post ID for later use because we wouldn’t want a custom query to change this. */

    $entry_views->post_id = $post->ID;

    Reply

Leave a Reply

By submitting a comment here you grant this site a perpetual license to reproduce your words and name/Web site in attribution.

Please use your real name or a pseudonym (i.e., pen name, alias, nom de plume) when commenting. If you add your site name, company name, or something completely random, I'll likely change it to whatever I want.

css.php