So I wanted to track page views without the cache of Jetpack site stats. When I was trying to achieve this, using the Jetpack site stats request. It wasn’t working quite like I wanted and I wanted something much simpler.
When doing some googling I found some solutions that didn’t seem quite complete. So I adjusted it some, and avoided the use of editing of template ‘singles.php’.
Frist we must create the functions that update pages meta with ‘post_views_count’ as key. The new condition of post_id being supplied is important when it comes to hooking our functions in the last function jd_wp_hook_pageviews
.
// function to grab how many views a page has. Supplying ID is optional.
function jd_get_post_view($post_id = null) {
if ($post_id == null){
$post_id = get_the_ID();
}
$count = (int) get_post_meta( $post_id, 'post_views_count', true );
return ($count);
}
//function to set page views to current viewed page if no id is supplied.
function jd_set_post_view($post_id = null) {
if ($post_id == null) {
$post_id = get_the_ID();
}
$count = (int) get_post_meta( $post_id, 'post_views_count', true );
$count++;
update_post_meta( $post_id, $key, $count );
}
// function that tells our server when to set page views and to what pages.
function jd_wp_hook_pageviews() {
// Total Website Visits
if (!is_admin()) {
// Exclude administrator views and update frontpage meta as placeholder.
if( !current_user_can( 'administrator' ) ){
$frontpage_id = get_option( 'page_on_front' );
jd_set_post_view($frontpage_id);
}
// Exclude front-page so we don't overwrite all page views.
if (!is_front_page()) {
// Exclude administrators from page views. Set the page view.
if( !current_user_can( 'administrator' ) ){
jd_set_post_view();
}
}
}
}
Now that our functions are created. We need to hook them accordingly. Instead of editing singles.php, we can hook our last function into the wp_head action. This will run our jd_wp_hook_pageviews function everytime a page is loaded.
add_action('wp_head', 'jd_wp_hook_pageviews');
To access our page views we can do the following in php.
$total_pageviews = jd_get_post_view(get_option( 'page_on_front' ));
$currentpage_pageviews = jd_get_post_view();
Completed Code
// function to grab how many views a page has. Supplying ID is optional.
function jd_get_post_view($post_id = null) {
if ($post_id == null){
$post_id = get_the_ID();
}
$count = (int) get_post_meta( $post_id, 'post_views_count', true );
return ($count);
}
//function to set page views to current viewed page if no id is supplied.
function jd_set_post_view($post_id = null) {
if ($post_id == null) {
$post_id = get_the_ID();
}
$count = (int) get_post_meta( $post_id, 'post_views_count', true );
$count++;
update_post_meta( $post_id, $key, $count );
}
// function that tells our server when to set page views and to what pages.
function jd_wp_hook_pageviews() {
// Total Website Visits
if (!is_admin()) {
// Exclude administrator views and update frontpage meta as placeholder.
if( !current_user_can( 'administrator' ) ){
$frontpage_id = get_option( 'page_on_front' );
jd_set_post_view($frontpage_id);
}
// Exclude front-page so we don't overwrite all page views.
if (!is_front_page()) {
// Exclude administrators from page views. Set the page view.
if( !current_user_can( 'administrator' ) ){
jd_set_post_view();
}
}
}
}
// Now hook to wp_head so it loads on every page load that is not admin.
add_action('wp_head', 'jd_wp_hook_pageviews');
Example Usage
This example shows page views for single posts. And displays at the top of the article, just like the one you are viewing now.
function output_before_article( $content ) {
if ( is_singular( 'post' ) ) {
$output = '<div style="text-align:right;"><i></i> '.jd_get_post_view().' VIEWS</div>';
$output .= $content;
$content = $output;
}
return $content;
}
add_filter( 'the_content', 'output_before_article' );
Advanced Usage
I’ve created a useful shortcode for my dedicated readers. This shortcode displays various site stats about your store. Eliminating the need to use PHP. All you have to after copying the code into your functions.php file is call the [
shortcode.site_stats
]
[restricted_content subscription=”799″]
// Type: Shortcode
// Author: JD Farrell
// Description: This shortcode displays various site stats.
// Parameters: Type = page/views/submissions/customers
add_shortcode('site_stats', 'site_stats_func');
function site_stats_func($atts) {
$atts = shortcode_atts(array(
'type' => '',
), $atts);
$type = $atts['type'];
if ($type == 'page') {
$content = '<div style="display:inline;">'.jd_get_post_view().'</div>';
} elseif ($type == 'views') {
$frontpage_id = get_option( 'page_on_front' );
$content = '<div style="display:inline;">'.jd_get_post_view($frontpage_id).'</div>';
} elseif ($type == 'submissions') {
$total = wp_count_posts()->publish;
$content = '<div style="display:inline;">'.$total.'</div>';
} elseif ($type == 'customers') {
$content = '<div style="display:inline;">'.count( get_users( array( 'role' => 'customer' ) ) ).'</div>';
} else {
$content = '<div style="display:inline;">Invalid</div>';
}
return $content;
}
[/restricted_content]
Examples
[site_stats type="page"
] // Shows current pages views [site_stats type="views"
] // Shows total website pageviews [site_stats type="submissions"
] // Shows # of articles on website [site_stats type="customers"
] // Displays current number of customers of store.