Footer Friendly WordPress Counters
This script works on all versions of WordPress.
It counts :
1) the number of visitors currently online
2) the total number of visitors
3) the number of postings
4) the total number of words in these postings
The counters appear neatly and discretely in the footer of your theme and takes up only one line of text. No graphics, no links, no spy meta crap. Just honest code.
Implementation goes in 2 steps :
Put the following stuff in your SQL query box in phpMyAdmin :
DROP TABLE IF EXISTS `websitestatistix`;
CREATE TABLE IF NOT EXISTS `websitestatistix` (
`timestamp` int(15) NOT NULL default \’0\’,
`ip` varchar(40) NOT NULL default \’\',
`count` int(15) NOT NULL default \’0\’,
PRIMARY KEY (`timestamp`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
INSERT INTO `websitestatistix` VALUES (1180707740, \’88.88.888.888\’, 111111);
This writes a fictuous IP-address (all those 8’s) and a total count of 111.111 visitors, which you of course can set to any numeric value – up to 15 digits – before executing the SQL query.
Put in the footer:
<div align=center>
<?php
function websitestatistix() {
$timeoutseconds = 3600;
$timestamp = time();
$timeout = $timestamp-$timeoutseconds;
$count_query = mysql_query(\"SELECT count FROM websitestatistix ORDER BY count DESC LIMIT 1\");
$count = mysql_fetch_row($count_query);
$count = $count[0];
$visitor_query = mysql_query(\"SELECT timestamp FROM websitestatistix WHERE ip = \’$_SERVER[REMOTE_ADDR]\’ ORDER BY timestamp DESC LIMIT 1\");
$visitor = mysql_fetch_row($visitor_query);
if($visitor[0] < $timeout) {
$count++;
}
$insert = mysql_query(\"INSERT INTO websitestatistix VALUES (\’$timestamp\’, \’$_SERVER[REMOTE_ADDR]\’, \’$count\’)\");
$delete = mysql_query(\"DELETE FROM websitestatistix WHERE timestamp<$timeout\");
$result = mysql_query(\"SELECT DISTINCT ip FROM websitestatistix\");
$online = mysql_num_rows($result);
echo \"online: $online | visits: $count\";
}
function statistix_post_word_count() {
global $wpdb;
$now = gmdate(\"Y-m-d H:i:s\",time());
$words = $wpdb->get_results(\"SELECT post_content FROM $wpdb->posts WHERE post_status = \’publish\’ AND post_date < \’$now\’\");
if ($words) {
foreach ($words as $word) {
$post = strip_tags($word->post_content);
$post = explode(\’ \’, $post);
$count = count($post);
$totalcount = $count + $oldcount;
$oldcount = $totalcount;
}
} else {
$totalcount=0;
}
echo \"words: \";
echo number_format($totalcount);
}
function statistix_post_count() {
global $wpdb;
echo \"posts: \";
echo $wpdb->get_var(\"SELECT COUNT(*) FROM $wpdb->posts WHERE post_status = \’publish\’ AND post_date_gmt < \’\" . gmdate(\"Y-m-d H:i:s\",time()) . \"\’\");
}
?>
<?php if(function_exists(websitestatistix)) { websitestatistix(); } ?> | <?php if(function_exists(statistix_post_count)) { statistix_post_count(); } ?> | <?php if(function_exists(statistix_post_word_count)) { statistix_post_word_count(); } ?>
</div>
On purpose I have not suggested to put that lengthy code in an includes, plugin or functions file, so the theme stays completely independent from the installation.
You can see the counters in action underneath this posting in the footer of this website.
Happy counting to you !