<?php
function theme_enqueue_scripts() {
wp_enqueue_script('frontend-ajax', get_template_directory_uri() . '/js/ajax-filter-posts.js', array('jquery'), null, true);
wp_localize_script('frontend-ajax', 'frontend_ajax_object',
array(
'ajaxurl' => admin_url('admin-ajax.php'),
'ajaxnonce' => wp_create_nonce('ajaxnonce'),
)
);
}
add_action('wp_enqueue_scripts', 'theme_enqueue_scripts');
add_action('wp_ajax_post_user_vote', 'ajax_user_vote_handler');
add_action("wp_ajax_nopriv_post_user_vote", "my_must_login");
function my_must_login() {
echo "You must log in to vote";
die();
}
function calculatefavs($user_id) {
$user_favorites_arr = get_user_meta($user_id, 'user_favorites', true);
$latest_fav_count = $new_favorite_count = $favorite_count = 0;
if (!empty($user_favorites_arr)) {
$favorite_count = count($user_favorites_arr);
foreach ($user_favorites_arr as $user_favorites_arr_single) {
$new_user_fav_arr[] = $user_favorites_arr_single;
}
}
return $new_user_fav_arr;
}
function ajax_user_vote_handler() {
if (isset($_POST['nonce']) && wp_verify_nonce($_POST['nonce'], 'ajaxnonce')) {
$voteid = $_POST['voteid'];
$user_id = get_current_user_id();
$fav_check = 'false';
$user_favorites_arr = calculatefavs($user_id);
$fav_number = get_post_meta($voteid, 'items_favorites', true);
if (is_null($user_favorites_arr)) {
$user_favorites_arr = array();
$fav_item_pos = false;
} else {
if (in_array($voteid, $user_favorites_arr)) {
$fav_number = $fav_number - 1;
} else {
$fav_number = $fav_number + 1;
}
update_post_meta($voteid, 'items_favorites', $fav_number);
if (is_array($user_favorites_arr)) {
$fav_item_pos = array_search($voteid, $user_favorites_arr);
} else {
$fav_item_pos = false;
}
}
if ($fav_item_pos === false) {
$user_favorites_arr[] = $voteid;
update_user_meta($user_id, 'user_favorites', $user_favorites_arr);
if(empty($fav_number)) {
update_post_meta($voteid, 'items_favorites', 1);
}
} else {
if (!empty($user_favorites_arr)) {
unset($user_favorites_arr[$fav_item_pos]);
} else {
$user_favorites_arr = array();
}
update_user_meta($user_id, 'user_favorites', $user_favorites_arr);
}
}
wp_die();
}
?>
jQuery(document).ready(function($) {
$('#vote').on('click', function(event) {
var voteid = $(this).data('id');
var data = {
action: 'post_user_vote',
voteid: voteid,
nonce: frontend_ajax_object.ajaxnonce
}
$.ajax({
url: frontend_ajax_object.ajaxurl,
type: 'POST',
data: data
})
.done(function(done) {
console.log(done);
})
.fail(function() {
console.log("error");
});
});
});
<p id="vote" data-id="<?php echo get_the_id(); ?>">Click here</p>
#vote {
color: #fff;
cursor: pointer;
background-color: #6c757d;
border-color: #6c757d;
display: inline-block;
font-weight: 400;
text-align: center;
white-space: nowrap;
vertical-align: middle;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
border: 1px solid transparent;
padding: .375rem .75rem;
font-size: 1rem;
line-height: 1.5;
border-radius: .25rem;
transition: color .15s ease-in-out,background-color .15s ease-in-out,border-color .15s ease-in-out,box-shadow .15s ease-in-out;
}
#vote:hover {
color: #fff;
background-color: #5a6268;
border-color: #545b62;
}