You’ll probably need to modify this a bit to ensure things get placed where you want them but here’s something I wrote that counts views on posts
and displays the view counts for administrators:
//ADD TRACKER FUNCTION TO ALL SINGLE VIEWS
function custom_hook_tracker( $post_id ) {
if( !is_single() ) return;
if( empty( $post_id ) ) {
global $post;
$post_id = $post->ID;
}
custom_track_post_views( $post_id );
}
add_action( 'wp_head', 'custom_hook_tracker' );
//ADD VIEW TRACKER
function custom_track_post_views( $postID ) {
$count_key = 'custom_view_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 );
}
//REMOVE THIS LINE IF YOU USE THE TEMPLATE TAG
if( is_single() && current_user_can( 'administrator' ) ) { //remove or adjust the '&& current_user_can( 'administrator' )' to modify who CAN see the counts
//ALSO REMOVE THIS LINE IF YOU USE THE TEMPLATE TAG
echo '<div style="position:absolute;top:2.75rem;width:auto;display:block;float:left;background:#FFF;border-radius:4px;box-shadow:#333 1px 1px 4px;padding:0.25rem 0.5rem;margin-left:-5px;color:#777;"><small>Views: '.$count.'</small></div>';
}
}
//REMOVE PRE-FETCHING TO KEEP COUNTS ACCURATE
remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);
Ok, I couldn’t just leave it – here’s the template tag:
function custom_view_counts() {
$count_key = 'custom_view_count';
$count = get_post_meta( $postID, $count_key, true );
echo '<div class="view-tracker"><small>Views: '.$count.'</small></div>';
}
Then to display it drop this in your theme files exactly where you want it to appear:
<?php custom_view_counts() ;?>
You’ll need to write css that address/styles/targets the .view-tracker
class/div.
You can basically build your own plugin, or just drop all of the above into your functions.php.