Display Post View Count Plugin for WordPress.com Stats

Steve May 8 Blogging , , with 11 and 0
Home/Blogging/Display Post View Count Plugin for WordPress.com Stats

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.



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.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

  • Jay
    on October 20th, 2015 at 22:40
    Reply

    After searching around finally got a working code…thanks

  • Jay
    on October 20th, 2015 at 22:39
    Reply

    Is it possible to duplicate this and add 2 counter , one for over all visits and another for today.

    Or can i modify the php code to do that ??

    thanks

  • SuzuKube
    on May 14th, 2015 at 00:15
    Reply

    Just to thank you for your plugin ! It work fine in 2015 🙂 You can see the result, for example, on that page : http://otakugame.fr/tuto-activer-le-store-hong-kong-sur-xbox-one/

    I will add something like “it’s a top 100 page” because it works only on the first 100 best posts of the website 🙂

    • Author
      Steve
      on May 15th, 2015 at 07:24
      Reply

      @SuzuKube

      Glad you found it useful.

      I will add something like “it’s a top 100 page” because it works only on the first 100 best posts of the website

      I think that may be a limitation of the WordPress.com Stats API.

      Regards, Steve

  • Barbara
    on September 16th, 2014 at 07:46
    Reply

    After trying several post view counter options, this one seemed perfect. Just one issue which may be a deal-breaker for the boss. Some of the post views don’t show up. For example, we have a breaking story on the site, the jet pack site stats say it’s already had 37 views, but the post view counter is still blank. I’ve tried clearing the site cache, no results. Any idea why this would be?

    • MATIAS
      on March 23rd, 2015 at 08:06
      Reply

      Same problem, i’ve tried with several post views counter and with this shows the jetpack stats but not instantly and so late

    • Author
      Steve
      on September 16th, 2014 at 12:24
      Reply

      @Barbara,

      This is unfortunately a limitation of the WordPress.com Stats API the plugin uses. I assume this is because the API is accessing a copy of the table(s), not the up-to-date one(s). I’ve noticed on new posts that views can be anything up to 24 hours behind.

      Regards, Steve.

  • Anshul Tewari
    on September 20th, 2013 at 01:45
    Reply

    Does it take a while for the stats to show up?

  • Chetan
    on November 14th, 2012 at 07:13
    Reply

    Exactly what I need but nothing appeared. I placed in my single post file. No luck. Tried all the settings.

    • Author
      Steve
      on November 14th, 2012 at 11:07
      Reply

      Hi Chetan,

      Silly question, I know, but you are using WordPress.com Stats, aren’t you? You might want to disable the plugin and give the manual solution a try: Displaying Statistics from WordPress.com Stats. Let me know what happens.

      Regards, Steve

Add comment

©Tech Otaku 2009 - 2017