post view count

How to add post view counter without plugin

If you want to display post view count in a simple way this post might help you. there are ton of free plugins for post view count in various method but bad things isthese plugin loaded unnecessary css and js file that obviously make issue on site loading time. In this post we will show you how simple can be post view count without plugins.

 

How to count view for post?

Here is the code you need to add in your theme  functions.php file.

 

function dartthemes_post_view_count(){

    // Check the post type you want to count view is_singular('post')
    if(is_singular('post')){

        // get the current post id
      $post_id = get_the_ID();

      // get the value previously view count, if empty will returned zero
      $post_view_count = get_post_meta($post_id,'view_count', true);

      //increase the value +1 with previous view count and update
      update_post_meta(get_the_ID(), 'view_count', ($post_view_count+1));

      //var_dump($post_view_count);
    }
}

add_action('wp_head','dartthemes_post_view_count');

This code will execute in single post, you can change post type under  is_singular('post') // try 'page' or you can also put array array('post','page')

Also this code will count view for every refresh, If you want to count view as unique you will need to check cookie.

How to count unique post view count?

here is the update code if you want to view count as unique.

function dartthemes_post_view_count(){

    // Check the post type you want to count view is_singular('post')
    if(is_singular('post')){

      // get the current post id
      $post_id = get_the_ID();
      var_dump($post_id);

      $cookie_name = 'view_count';
      if(isset($_COOKIE[$cookie_name.'_'.$post_id])){

          // will do nothing if cookie exist.

      }
      else{

        setcookie( $cookie_name.'_'.$post_id, $post_id, time() + (86400 * 30)); // 86400s = 1 day

        // get the value previously view count, if empty will returned zero
        $post_view_count = get_post_meta($post_id,'view_count', true);

        //increase the value +1 with previous view count and update
        update_post_meta(get_the_ID(), 'view_count', ($post_view_count+1));


      }

      //var_dump($post_view_count);

    }
}

add_action('wp_head','dartthemes_post_view_count');

 

How to display view count?

After adding the above code you will see there is meta field create with view count when you visit the post. you will need to display that count uder post. here is the code you can display view count under post content.

function dartthemes_post_view_count_display($content){

    // Check the post type you want to count view is_singular('post')
    if(is_singular('post')){
      // get the current post id
      $post_id = get_the_ID();

      // get the value previously view count, if empty will returned zero
      $post_view_count = get_post_meta($post_id,'view_count', true);

      $count_html= '';
      $count_html.='<div class="view-count">';

      $count_html.=__('Total view count: ','your-text-domain'). $post_view_count;
      $count_html.='</div>';


      return $content.$count_html;

    }
}

add_filter('the_content','dartthemes_post_view_count_display');

I hope code will help you to simplay way to display post view count.