I'm using RealHomes Wordpress theme and I want to remember the clicks on each property, but only in one session.
For example, I go to a website to look at some properties, and I click on a few of them, and later when I refresh the page it would say that those properties have been seen, maybe just a text saying "Seen".
And later, or tomorrow, when I go to that same page, all those properties shouldn't have a message "Seen", I want to reset the clicks/views.
I have these two functions in my functions.php file, the getPostViews can be called where you want to display the counter, and setPostViews is called on the beginning of the single.php property file.
function setPostViews($postID) {
$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);
}
add_action('end_session_action','end_session');
}
function getPostViews($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 View";
}
return $count;
}
I desided to display the clicks on the homepage, next to each property.
Image: property hasn't been seen
My question is, how can I reset this meta values on each new session?
I also found this code that displays the last time a user visited a website, it resets the clock everytime.
function viewed_cookie($postID) {
// Time of user's visit
date_default_timezone_set("Europe/Ljubljana");
$visit_time = date('d.m.Y. G:i');
// Check if cookie is already set
if(isset($_COOKIE['visited_cookie'])) {
// Do this if cookie is set
function postViewed() {
// Use information stored in the cookie
$lastvisit = $_COOKIE['visited_cookie'];
$string .= $lastvisit;
// Delete the old cookie so that we can set it again with updated info
unset($_COOKIE['visited_cookie']);
return $string;
}
} else {
// Do this if the cookie doesn't exist
function postViewed() {
$string .= 'New here? Check out these resources...' ;
return $string;
}
}
add_shortcode('viewed', 'postViewed');
// Set or Reset the cookie
setcookie('visited_cookie', $visit_time);
}
add_action('init', 'viewed_cookie');
How can I merge the set/get functions and use a cookie like in the last example?