Created
July 5, 2020 15:44
-
-
Save zexeder/245c20ff10a549dfbb36f66b59bb3138 to your computer and use it in GitHub Desktop.
WP post views counter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if ( ! function_exists( 'function_name_given' ) ) : | |
/** * get the value of view. */ | |
function function_name_given($postID) { | |
$count_key = 'wpb_post_views_count'; | |
$count = get_post_meta($postID, $count_key, true); | |
if($count ==''){ | |
$count = 1; | |
delete_post_meta($postID, $count_key); | |
add_post_meta($postID, $count_key, '1'); | |
} else { | |
$count++; | |
update_post_meta($postID, $count_key, $count); | |
} | |
} | |
endif; | |
<!--put it in single.php before endwhile loop --> | |
<?php function_name_given(get_the_ID()); ?> | |
<!--put it where you need it--> | |
<div > <i class="fa fa-eye"></i> | |
<?php | |
if ( get_post_meta( get_the_ID() , 'wpb_post_views_count', true) == '') { | |
echo '0' ; | |
} else { | |
echo get_post_meta( get_the_ID() , 'wpb_post_views_count', true); | |
}; | |
?> | |
</div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Post Views Counter | |
/* Подсчет количества посещений страниц | |
---------------------------------------------------------- */ | |
add_action('wp_head', 'custom_postviews'); | |
function custom_postviews() { | |
/* ------------ Настройки -------------- */ | |
$meta_key = 'postviews_counter'; // Ключ мета поля, куда будет записываться количество просмотров. | |
$who_count = 0; // Чьи посещения считать? 0 - Всех. 1 - Только гостей. 2 - Только зарегистрированных пользователей. | |
$exclude_bots = 0; // Исключить ботов, роботов, пауков и прочую нечесть :)? 0 - нет, пусть тоже считаются. 1 - да, исключить из подсчета. | |
global $user_ID, $post; | |
if(is_singular()) { | |
$id = (int)$post->ID; | |
static $post_views = false; | |
if($post_views) return true; // чтобы 1 раз за поток | |
$post_views = (int)get_post_meta($id,$meta_key, true); | |
$should_count = false; | |
switch( (int)$who_count ) { | |
case 0: $should_count = true; | |
break; | |
case 1: | |
if( (int)$user_ID == 0 ) | |
$should_count = true; | |
break; | |
case 2: | |
if( (int)$user_ID > 0 ) | |
$should_count = true; | |
break; | |
} | |
if( (int)$exclude_bots==1 && $should_count ){ | |
$useragent = $_SERVER['HTTP_USER_AGENT']; | |
$notbot = "Mozilla|Opera"; //Chrome|Safari|Firefox|Netscape - все равны Mozilla | |
$bot = "Bot/|robot|Slurp/|yahoo"; //Яндекс иногда как Mozilla представляется | |
if ( !preg_match("/$notbot/i", $useragent) || preg_match("!$bot!i", $useragent) ) | |
$should_count = false; | |
} | |
if($should_count) | |
if( !update_post_meta($id, $meta_key, ($post_views+1)) ) add_post_meta($id, $meta_key, 1, true); | |
} | |
return true; | |
} | |
<!-- show --> | |
<?php echo get_post_meta($post->ID,'postviews_counter',true); ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment