Display your WordPress blog Statistics

WordPress Recent-Comments Without a Plugin

One of the things I wanted to implement in my new website theme, was the display of the blog statistics. After the code snippet to display recent comments without a plugin, I really wanted to give something new to my footer.

There are several reasons why you should want to display your website statistics. First you might have a page where you display your blog statistics, like feed readers, total posts, comments, etc. Instead of updating it manually every couple of months you can have it automated. Or like me, you can display your statistics in the footer or even in your sidebar, for curiosity and/or to show your readers your blog history / performance. Whichever is your reason, having something automated in providing more info to your readers, enriches your blog.

The code was very simple to implement, just took me a few visits to the wordpress codex and to my WordPress database to get the variables I needed. This code is capable of displaying the Post Count, Page Count, the Comment Count, the Comments Ratio per Post (Comments / Total Posts) and the Number of Registered Users.

To display the number of your RSS Readers, you will need first to write some PHP functions to your wordpress theme file. The file is called functions.php. This was copied from WPRecepies.

Just insert the code into your functions.php file. Replace Paintbits to your feedburner account name.

function RSScount()
{
$fburl="https://feedburner.google.com/api/awareness/1.0/GetFeedData?uri=Paintbits";
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $fburl);
$stored = curl_exec($ch);
curl_close($ch);
$grid = new SimpleXMLElement($stored);
$rsscount = $grid->feed->entry['circulation'];
return $rsscount;
}

Whenever you want to make a call to your RSS Feed count, just write: RSScount(); inside PHP code tags of course. You’ll see how in the final code. Alternatively to use this code, you can use a plugin like Feedburner Circulation. The advantage is that with a plugin like this you will have cache and error protection on your Feed Count text display.

Download Feedburner Circulation

So, now that we can display our Feed Count in full text, we just need to write the other variables assigns to get the rest of our website statistics.

< ?php
$count_posts = wp_count_posts(); 
$posts = $count_posts->publish; /* Gets the post count */
$count_pages = wp_count_posts('page');
$pages = $count_pages->publish; /* Gets the page count */
$count_comments = get_comment_count();
$comments = $count_comments['approved']; /* Gets the approved comment count */
$rss_readers = RSScount(); /* Calls the RSScount function to display your Feed Count */
$user_count = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->users;"); /* Gets the number of registered users*/
?>

In order to display your statistics, you just call and print your variables. This is how I did it in my footer:

<ul>
<li><a href="/archives/" title="Written posts">Articles</a>< ?php echo ": ".$posts."" ?></li>
<li><span class="footer_comm_author" title="Pages published">Pages</span>< ?php echo ": ".$pages."" ?></li>
<li><span class="footer_comm_author" title="User comments">Comments</span>< ?php echo ": ".$comments."" ?></li>
<li><span class="footer_comm_author" title="Comments per Post Ratio">Comments Ratio</span>< ?php echo ": ".round($comments/$posts)."" ?></li> /* Calculates the Comment Ratio */
<li><a href="http://www.paintbits.com/feed" title="Feed readers">Readers</a>< ?php echo ": ".$rss_readers."" ?></li>
<li><span class="footer_comm_author" title="Registered users">Members</span>< ?php echo ": ".$user_count."" ?></li>
</ul>

All put together, this is what I’ve used in my footer (don’t forget the RSScount function you need to add to your functions.php theme file):

<div class="footer_stats">
<h2>Statistics</h2>
< ?php
$count_posts = wp_count_posts();
$posts = $count_posts->publish;
$count_pages = wp_count_posts('page');
$pages = $count_pages->publish;
$count_comments = get_comment_count();
$comments = $count_comments['approved'];
$rss_readers = RSScount();
$user_count = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->users;");
?>




<ul>
<li><a href="/archives/" title="Written posts">Articles</a>< ?php echo ": ".$posts."" ?></li>
<li><span class="footer_comm_author" title="Pages published">Pages</span>< ?php echo ": ".$pages."" ?></li>
<li><span class="footer_comm_author" title="User comments">Comments</span>< ?php echo ": ".$comments."" ?></li>
<li><span class="footer_comm_author" title="Comments per Post Ratio">Comments Ratio</span>< ?php echo ": ".round($comments/$posts)."" ?></li>
<li><a href="http://www.paintbits.com/feed" title="Feed readers">Readers</a>< ?php echo ": ".$rss_readers."" ?></li>
<li><span class="footer_comm_author" title="Registered users">Members</span>< ?php echo ": ".$user_count."" ?></li>
</ul>
</div>

I can’t say this enough, WordPress Codex Funtion Reference is one of the best tools you can have when developing new code for your WordPress blog. Explore it a bit and you’ll probably find what you are looking for. WordPress is amazing and saves you a lot of PHP coding to a lot of commonly used tasks. This is why we all love WordPress.

Hope this is useful to you in some way.

Tags:

 

Copyright © 2008 - 2010 Paintbits. All rights reserved. Hosted by WebHS.