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.
to 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
Download this file- Rename it viewcount.php and copy it to your
/wp-content/pluginsfolder. - 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 />’ ); ?>