Display Post View Count Plugin for WordPress.com Stats

D

Some while back I described a method for retrieving and displaying individual post view counts from WordPress.com. Having proved quite popular, I thought I’d wrap this functionality in a simple plugin.

display-post-view-count.zip9KB30th September 2017

Prerequisite

The plugin requires your site to have WordPress.com Stats activated either as part of the Jetpack plugin or the previous stand-alone plugin version.

 

Installation

Extract all files from the ZIP file, making sure to keep the file/folder structure intact, and then upload it to the /wp-content/plugins/ directory. Go to the admin area of your WordPress install and click on the Plugins menu. Click on Activate for the Display Post View Count plugin.

 

Settings

Period
Filter to display view counts for a specific period. The options are All Time, Last 24 Hours, Last 7 Days or Last Month. The default is All Time.

Display To All
If set to No, view counts are only displayed to logged-in users. If Yes, view counts are displayed to visitors as well as logged-in users. The default is No.

Include Link
If set to Yes, the view count includes a link to the individual post’s statistics at WordPress.com. The link is only included for Admin users. The default is Yes.

Include Wrapper
If set to Yes, the output is wrapped in a <span> tag. The default is No.

Element Class Name
An optional class name for the <span> tag. The default is dpvc_post_views.

Before Display Text
An optional string to prepend to the Display Text. Can include an HTML element. The default is blank.

Display Text
The text used to display the individual post’s view count. The default is %VIEW_COUNT% views. The variable %VIEW_COUNT% is replaced with the post’s actual view count.

After Display Text
An optional string to apppend to the Display Text. Can include an HTML element. The default is blank.

Link Hover Text
The text shown when the mouse hovers over the link to the individual post’s statistics at WordPress.com. The default is Show detailed stats for %POST_TITLE% at WordPress.com [%PERIOD%]. The variable %POST_TITLE% is replaced with the post’s title. The variable %PERIOD% is replaced with the period the view counts are for.

Surround post title with single quotes
If checked, encloses the post’s title in single quotes to differentiate it from the surrounding text. The default is checked.

Restore defaults upon plugin deactivation/reactivation
If checked, settings will return to their default values if the plugin is reactivated. The default is un-checked.

 

Usage

Place the following php code where you want the view counts to display. Typically this will be in your theme’s archive.php, post.php, page.php or single.php files.

<?php if ( function_exists('dpvc_post_views') ) { echo do_shortcode('[display-post-view-count]'); } ?>

 

The display-post-view-count shortcode accepts two parameters: display and link which are synonymous with the Display To All and Include Link settings respectively. Each parameter can be set to either 0 (No) or 1 (Yes). If used, they overwrite their respective setting’s value.

As an example the following code displays view counts to everyone, but no link is included:

<?php if ( function_exists('dpvc_post_views') ) { echo do_shortcode('[display-post-view-count display=1 link=0]'); } ?>

The code must be placed within the WordPress Loop as follows:

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
 
.....
 
	<?php if ( function_exists('dpvc_post_views') ) { echo do_shortcode('[display-post-view-count display=1 link=0]'); } ?>
 
.....
 
<?php endwhile; else: ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>

 

UPDATE 12th May 2013: For anyone experiencing difficulty getting post view counts to display, please see Why Aren’t Post View Counts Displayed? below for a possible cause and solution.

 

Why Aren’t Post View Counts Displayed?

One reason may be due to the functionality of the Jetpack plugin’s Stats module upon which the Display Post View Count Plugin relies.

The Stats module, wp-content/plugins/jetpack/modules/stats.php, contains a function named stats_str_getcsv which attempts to create a temporary file in the folder specified for such files in the PHP configuration. If this folder doesn’t exist temporary files can’t be created and the function simply exits effectively returning no post view count data.

function stats_str_getcsv( $csv ) {
	if ( !$temp = tmpfile() ) // tmpfile() automatically unlinks
		return false;

	$data = array();

	fwrite( $temp, $csv, strlen( $csv ) );
	fseek( $temp, 0 );
	while ( false !== $row = fgetcsv( $temp, 2000 ) )
		$data[] = $row;
	fclose( $temp );

	return $data;
}

The stats_str_getcsv function of the Jetpack plugin’s Stats module

 

 

To find the location in the PHP configuration where temporary files should be created, first copy the code below and save it as phpinfo.php in the root directory of your site.

<?php 
	phpinfo();
?>

Type the URL http://mysite.com/phpinfo.php in a browser to display the PHP configuration settings. Locate any of the following PHP variables: _SERVER[“TMPDIR”], _SERVER[“TEMP”] or _SERVER[“TMP”] . They should all contain the same value, that being the location for creating temporary files: /path/to/tmp

It is highly likely that the tmp folder has the wrong permissions or doesn’t exist. Create this folder if necessary and set the permissions to 705. Wait a minute or two for the updates to take effect after which post view counts should be displayed.

 

Change Log

 

0.9.4The dpvc_post_views function assigned to the display-post-view-count shortcode
now returns the output instead of echoing it.
2017-09-30
0.9.3Added option to wrap output in <span> tag with optional class name
Added optional Before and After text to Display Text
2014-07-03
0.9.2Added warning message regarding requisite plugins
Added sample of raw post view data to settings page
2013-09-23
0.9.1Added filter to display view counts for a specific period2013-05-15
0.9.0Initial Release2012-05-08

About the author

A native Brit exiled in Japan, Steve spends too much of his time struggling with the Japanese language, dreaming of Fish & Chips and writing the occasional blog post he hopes others will find helpful.

11 responses

11 Comments

Steve

Recent Comments

Recent Posts

Archives