ArticlesForumDownload AboutContact

boakes.org

nice of you to drop by. tea?

Tags: Releases, WordPress

extending stattraq

September 24th, 2004, by Rich.

extending stattraq

one of the most useful things that stattraq author randy has done (apart from writing stattraq) is write up a few words about hacking wordpress. and as a kind of review of these notes, i had a go at knocking up a wordpress plugin that makes a database query. i wrote my first wordpress plugin yesterday, so this is my second. it’s the first one i’ve done that accessed the db, and the first one that i’ll stick out into the real world.

my requirements:
along with the normal metadata which can be seen about any post, i’d like to be able to show how many times that post has been viewed. additionally, by default, i want this information to be restricted to administrators.

output from thre ViewCount pluginto cut a not very long story short, it works. there were a few teething troubles as i worked out how to get the current post ID - and there may be a better way of generating the relative url (please comment if you know of one), but aside from that, i’m a happy bunny because i get my home page, and every posting, with integrated live usage stats.

Installation

  1. Download this fileDownload this file
  2. Rename it viewcount.php and copy it to your /wp-content/plugins folder.
  3. using wp-admin, enable the plugin

Usage

In my index.php file, within the loop, and in amongst all the other metadata code, I use the plugin as follows.

<?php rjb_get_view_count( $post->ID, ”, ‘ views<br />’ ); ?>

14 Responses to “extending stattraq”

Pages: 12»

  1. 1
    Trackback from: StatTraq » Add Post View Counts To Your WordPress Page
  2. 2
    Trackback from: Jonathan Foucher
  3. 3
    Jonathan Says:

    The $url variable can be set much more efficiently as such :

    $url=$_SERVER['PHP_SELF'].”?”.$_SERVER['QUERY_STRING'];

    and then it doesn’t need to be passed as a parameter to the function anymore. Try it and tell me if it works for you. It’s ok with my permalink setup anyway…

    Rich writes: Thanks for the hint Jonathan - I’ve tried this and got nothing so it may be down to having url rewriting on… (it’s off for me - hints anyone?)… or something. In any case the way to use this would be outside the function so that it can still show the view count for multiple posts on a single page. ie. when i see my index page with the latest posts, i can also see the view count for every post - putting this in the function would stop me from doing that.

  4. 4
    Nikolas Kyramarios Says:

    Well, my site index.php is in http://www.kyramarios.gr/diary and my wp installation is at http://www.kyramarios.gr/wp
    How should I edit the plugin and the command in order to get it to work ?
    ps. Inside the stattraq stats (which work ok) the url of the blog is wrong. It points at the installation instead of the /diary/ directory
    Please help.
    Thanks

    Rich writes: following the comments of Jonathan and Nikolas, I’ve had a look to see if things can be simplified and happily they can, so above you’ll notice that the code is now version 0.0.2 - and the URL’s are gone - the only thing you need to pass to the code now is the ID of the post and it does the rest - this should, i think, i hope, handle both the issues identified. The new code works for me, so if anyone would care to try it & give me a thumbs up/down would be most helpful.

  5. 5
    Nikolas Kyramarios Says:

    Well unfortunatelly it doesnt work. I guess I am stuck.
    Strange thing is that from inside stattraq posts gets the correct id (and url) so I think my db is ok. Any ideas ?
    Thanks for looking into it.

    Rich writes:Nikolas later contacted me by email to confirm that it’s now working - and it sounds like it may have been a content-cacheing issue. One other reason why it might appear to “not work” is if you’re logged in on one browser, but anonymous on another, this will therefore show you the admin view (with the count) and the end user view (without the count) respectively.

  6. 6
    Javier Says:

    Hi,

    I’ve just installed your 0.0.3 version and wordpress kicks out the following error:

    Parse error: parse error, unexpected T_VARIABLE in /var/www/wordpress/wp-content/plugins/viewcount_0.0.3.php on line 42

    right after activating the plugin. Any hints?

  7. 7
    Rich Says:

    Hi Javier - the first thing to check is whether there is a space after ?> on line 70. The > symbol must be the last character in the file (no carriage returns either).

  8. 8
    Javier Says:

    Rich,

    I’ve checked it using the vi editor, and the > is the last symbol in my file. It’s pretty weird since it seems to be working for the people out there :-(

    Javier.

  9. 9
    Trackback from: links for 2006-02-13 at HawkEye
  10. 10
    ktula Says:

    Is there a reason why the viewcount is only visible when i login to WP as administrator? If i log out, i don’t see the viewcount per post.

Pages: 12»

Leave a Reply