Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if ( ! function_exists( 'pietergoosen_get_post_views' ) ) :
- function pietergoosen_get_post_views($postID){
- $count_key = 'post_views_count';
- $count = get_post_meta($postID, $count_key, true);
- if($count==''){
- delete_post_meta($postID, $count_key);
- add_post_meta($postID, $count_key, '0');
- return 0;
- }
- return $count;
- }
- endif;
- // function to count views.
- if ( ! function_exists( 'pietergoosen_update_post_views' ) ) :
- function pietergoosen_update_post_views($postID) {
- if( !current_user_can('administrator') ) {
- $user_ip = $_SERVER['REMOTE_ADDR']; //retrieve the current IP address of the visitor
- $key = $user_ip . 'x' . $postID; //combine post ID & IP to form unique key
- $value = array($user_ip, $postID); // store post ID & IP as separate values (see note)
- $visited = get_transient($key); //get transient and store in variable
- //check to see if the Post ID/IP ($key) address is currently stored as a transient
- if ( false === ( $visited ) ) {
- //store the unique key, Post ID & IP address for 12 hours if it does not exist
- set_transient( $key, $value, 60*60*12 );
- // now run post views function
- $count_key = 'post_views_count';
- $count = get_post_meta($postID, $count_key, true);
- if($count==''){
- $count = 0;
- delete_post_meta($postID, $count_key);
- add_post_meta($postID, $count_key, '0');
- }else{
- $count++;
- update_post_meta($postID, $count_key, $count);
- }
- }
- }
- }
- endif;
- remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);
- // initialzing
- function wpb_track_post_views2 ($post_id) {
- if ( !is_single() ) return;
- if ( empty ( $post_id) ) {
- global $post;
- $post_id = $post->ID;
- }
- pietergoosen_update_post_views($post_id);
- }
- add_action( 'wp_head', 'wpb_track_post_views2');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement