I’m using a custom number field (generated by Jet Engine plugin) to track the number of times a page was loaded.
On my functions.php
file I am using this code:
$count = get_post_meta("706", 'counter', true );
if(!is_admin() && !current_user_can('administrator')){
$count++;
update_post_meta("706", 'counter', $count );
}
‘counter’ is the field name.
I am using the if(!is_admin)
so it will not count my back end testings.
My main issue is that the counter is not consistent, and although for most of the times it counts in steps of 1
, it sometimes skips and count 2
, 3
or 4
on a single page load.
This is a link to my test page:
https://oferziv.com/ofer/test/test3/
What am I missing here?
2
Answers
Usually, you’ll want to wrap everything inside a proper hook, like so:
About how to use hooks, see:
For checking user privileges, see:
Like i said, i always use the
wp_head
action hook and it works seamlessly fine!