public
Last active

simple view counter for wordpress plugin

  • Download Gist
gistfile1.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
<?php
/*
Plugin Name: Simple Post Views
Plugin URI: http://en.bainternet.info
Description: Simple And lightweight plugin to track post views.
Version: 0.2
Author: Bainternet
Author URI: http://en.bainternet.info
*/
if ( !class_exists('PostViews')){
class PostViews{
static $meta_key = "_post_views_count"
//class constractor
public function __construct(){
//auto count view on the_content function.
add_filter('the_content',array($this,'setPostViews'));
//add shortcodes
add_shortcode("TOP_VIEWED",array($this,'top_viewed'));
add_shortcode("GET_VIEWS"array($this,'top_viewed'));
//add colums to list view
add_filter('manage_posts_columns', array($this,'posts_columns_id'), 5);
add_action('manage_posts_custom_column', array($this,'posts_custom_id_columns'), 5, 2);
 
//fixed ajax counts for cached pages
add_action('wp_ajax_simple_post_views',array($this, 'increment_views'));
add_action('wp_ajax_nopriv_simple_post_views',array($this, 'increment_views'));
}
//add views column to posts listing
public function posts_columns_id($defaults){
$defaults['post_views'] = __('Views');
return $defaults;
}
//render views column
public function posts_custom_id_columns($column_name, $id){
if($column_name === 'post_views'){
echo $this->getPostViews(get_the_ID());
}
}
//function to get the views
public function getPostViews($postID = null){
if (null == $postID){
global $post;
$postID = $post->ID;
}
$count = get_post_meta($postID, $this->meta_key, true);
if($count==''){
delete_post_meta($postID, $this->meta_key);
add_post_meta($postID, $this->meta_key, '0');
$re = 0;
}else{
$re = $count;
}
return apply_filters("Post_Views_get_views",$re,$postID);
}
//function to set the view count
public function setPostViews($content, $postID = null) {
if (null == $postID){
global $post;
$postID = $post->ID;
}
if(defined('WP_CACHE') && WP_CACHE) {
wp_print_scripts('jquery');
$content .= '<script type="text/javascript">
/* <![CDATA[ */
jQuery.ajax({type:"GET",url: "'.admin_url('admin-ajax.php', (is_ssl() ? 'https' : 'http')).'", data: "post_views_id="'.$postID.'"&action=simple_post_views",cache:false});
/* ]]> */
</script>
<!-- End Of Script Generated By Simple Post Views -->';
}else{
$count = get_post_meta($postID, $this->meta_key, true);
if($count==''){
$count = 1;
delete_post_meta($postID, $this->meta_key);
add_post_meta($postID, $this->meta_key, '1');
}else{
$count++ ;
update_post_meta($postID, $this->meta_key, $count);
}
}
return $content;
}
//function to get views by shortcode.
public function top_viewed($atts, $content = null) {
extract(shortcode_atts(array(
"id" => null
), $atts));
return $this->getPostViews($id);
}
/**
* increment_views ajax callback for cache enabled sites.
* @return void
*/
public function increment_views(){
if (isset($_GET['post_views_id'])){
$count = get_post_meta($postID, $this->meta_key, true);
if($count==''){
$count = 1;
delete_post_meta($postID, $this->meta_key);
add_post_meta($postID, $this->meta_key, '1');
}else{
$count++ ;
update_post_meta($postID, $this->meta_key, $count);
}
echo $count;
die();
}
die();
}
 
 
/**
* shortcode handler for top viewed
* usage: [TOP_VIEWED count="5"]
* this will list top five viewed posts
*/
public function top_viewed($atts, $content = null) {
extract(shortcode_atts(array(
"count" => '5'
), $atts));
 
global $wp_query,$paged,$post;
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query(array(
'posts_per_page' => $count,
'order' => 'DESC',
'orderby' => 'meta_value_num',
'meta_key' => $this->meta_key
));
ob_start();
?>
<ul class="loop">
<?php have_posts()) : $wp_query->the_post(); ?>
<li><a href="<?php the_permalink(); ?>" rel="bookmark"><?php the_title(); ?></a></li>
<?php endwhile; ?>
</ul>
<?php
$wp_query = null;
$wp_query = $temp;
wp_reset_query();
$content = ob_get_contents();
ob_end_clean();
 
return $content;
}
}//end class
}
$Vcounter = new PostViews();

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.