Ed's Big Plans

Computing for Science and Awesome

Archive for the ‘Wordpress Plugins’ tag

Add ‘Day/Week/Month’ to WordPress.com Stats dashboard widget

without comments

Brief: The WordPress.com Stats plug-in does exactly what it’s supposed to. In the Dashboard, it shows you your top posts, top search queries and your most active posts. The problem here is that it won’t say for what period of time it’s showing you these lists. In this post, I’ll show you a quick hack to do just that so that each of those list titles will now be accompanied by the text Day, Week or Month.

In case you didn’t know, you can change over which period you want each of those lists to be shown by mousing over the top-right corner of the WordPress.com Stats Dashboard widget and clicking the revealed text, configure. Below are two images that illustrate our objective today.

The screenshot on the left (before) has the default list headers and the one on the right (after) has period labels.

The PHP file you have to edit is wp-content/plugins/stats/stats.php — you can open this file on your filesystem — or if your WordPress installation is hosted elsewhere, you can use the built-in editor. To get to this file using the built-in editor, go to your dashboard (wp-admin), then click on PluginsEditor — then in the drop-down labelled Select plugin to edit, choose WordPress.com Stats.

Once you have access to the file, look for a block of HTML that looks like the following. There’s only one div with the id stats-info — so you can use that to string search over the file. The ellipses (“…”) means that I’m skipping several irrelevant lines.

<div id="stats-info">
	<div id="top-posts" class='stats-section'>
		<div class="stats-section-inner">
		<h4 class="heading"><?php _e( 'Top Posts' , 'stats'); ?></h4>
		...
		<h4 class="heading"><?php _e( 'Top Searches' , 'stats'); ?></h4>
		...
		<h4 class="heading"><?php _e( 'Most Active' , 'stats'); ?></h4>
		...
		</div>
	</div>
</div>

You’ll next need to add the following line directly above <div id=”stats-info”> . What it does is rebuild a dictionary (associative array) so that a period of one means “Day”, seven means “Week”, and thirty-one means “Month”. If you’ve discovered a different dictionary that has these values keyed in reverse, let me know since I couldn’t find it.

<?php $re_periods = array('1' => 'Day', '7' => 'Week', '31' => 'Month'); ?>

Finally, replace the h4 elements in the original code with the h4 elements in the below code. This completes the customization.

<div id="stats-info">
	<div id="top-posts" class='stats-section'>
		<div class="stats-section-inner">
		<h4 class="heading">
	<?php _e( 'Top Posts &middot; '.$re_periods[$options[top]] , 'stats'); ?>
		</h4>
		...
		<h4 class="heading">
	<?php _e( 'Top Searches &middot; '.$re_periods[$options[search]] , 'stats'); ?>
		</h4>
		...
		<h4 class="heading">
	<?php _e( 'Most Active &middot; '.$re_periods[$options[active]] , 'stats'); ?>
		</h4>
		...
		</div>
	</div>
</div>

The above code uses the $options dictionary to look for the correct period, then uses that period in our new $re_periods dictionary to look for the correct name. That name is then concatenated to the string inside the h4 element and serves as our new title or list header inside the dashboard.

Enjoy!

Eddie Ma

January 28th, 2011 at 12:52 pm