WordPress update_comment_meta onclick not working with Ajax
Question
I’m attempting to update_comment_meta via Ajax call in WordPress,
but no data is returned and the comment_meta field is not being updated.
Fun.php
add_action( 'wp_enqueue_scripts', 'comment_voting_enqueue_scripts' );
function comment_voting_enqueue_scripts() {
wp_enqueue_script( 'comment_meta_voting-js', get_template_directory_uri() . '/js/ajax-vote.js', array( 'jquery' ), '0.5', false );
wp_localize_script( 'comment_meta_voting-js', 'commentMetaVoting', array(
'ajaxurl' => admin_url( 'admin-ajax.php' ),
) );
}
add_action( 'wp_ajax_nopriv_comment_meta_voting', 'comment_meta_voting' );
add_action( 'wp_ajax_comment_meta_voting', 'comment_meta_voting' );
function comment_meta_voting() {
$comment_id = $_POST['vote'];
$vote_total = get_comment_meta($comment_id , 'vote', true);
++$vote_total;
update_comment_meta($comment_id , 'vote', $vote_score );
die();
}
ajax-vote.js
(function($) {
'use strict';
$(document).on('click', '.sl-button', function() {
var button = $(this);
var cmnt_id = button.attr('data-post-id');
var security = button.attr('data-nonce');
$.ajax({
type: 'POST',
url: commentMetaVoting.ajaxurl,
data : {
action : 'comment_meta_voting',
post_id : cmnt_id,
nonce : security,
},
success: function(response){
console.log('tst');
}
});
return false;
});
})(jQuery);
and in wp_list_comments callback i add core function where i want to show the link to call:
<a href="<?php echo admin_url( 'admin-ajax.php?action=comment_meta_voting' . '&cmnt_id=' . $commentid . '&nonce=' . $nonce ); ?>" class="sl-button" data-nonce="<?php echo $commentid ; ?>" data-post-id="<?php echo $commentid ; ?> ">click</a>
Finally i get console.log(‘tst’); in thee console.. so i think its correct, no data is returned and the comment_meta field is not being updated..
any help?
0
4 months
0 Answers
15 views
0
Leave an answer
You must login or register to add a new answer .