admin管理员组文章数量:1415645
I'm trying to create a simple ajax-based comment upvoting system for Wordpress. I've managed to build most of it, but now it seems that most of the time the upvotes don't register. They do register sometimes (but only maximum of 2, even though I did not implement a limit), and the Ajax call gets a successful response (so the PHP function is executed I guess).
Any ideas where I'm going wrong (this is my first Ajax project)?
PHP function:
add_action( 'wp_ajax_comment_meta_update_vote', 'comment_meta_update_vote' );
function comment_meta_update_vote( ) {
check_ajax_referer( 'upvote_nonce' );
$comment_id = $_POST['comment_id'];
$vote_score = get_comment_meta($comment_id , 'vote', true);
++$vote_score;
update_comment_meta($comment_id , 'vote', $vote_score, true );
$response['type'] = 'success';
$response = json_encode( $response );
echo $response;
die();
}
Javascript Function:
function updateVote( comment_id ){
jQuery(function($) {
$.ajax({
type : 'post',
dataType : 'json',
url : upvote_ajax.ajax_url,
data : {
action: 'comment_meta_update_vote',
comment_id: comment_id,
_ajax_nonce: upvote_ajax.nonce
},
success: function( response ) {
if( 'success' == response.type ) {
// console.log('success');
}
else {
// alert( 'Something went wrong, try logging in!' );
}
}
}) })};
I'm trying to create a simple ajax-based comment upvoting system for Wordpress. I've managed to build most of it, but now it seems that most of the time the upvotes don't register. They do register sometimes (but only maximum of 2, even though I did not implement a limit), and the Ajax call gets a successful response (so the PHP function is executed I guess).
Any ideas where I'm going wrong (this is my first Ajax project)?
PHP function:
add_action( 'wp_ajax_comment_meta_update_vote', 'comment_meta_update_vote' );
function comment_meta_update_vote( ) {
check_ajax_referer( 'upvote_nonce' );
$comment_id = $_POST['comment_id'];
$vote_score = get_comment_meta($comment_id , 'vote', true);
++$vote_score;
update_comment_meta($comment_id , 'vote', $vote_score, true );
$response['type'] = 'success';
$response = json_encode( $response );
echo $response;
die();
}
Javascript Function:
function updateVote( comment_id ){
jQuery(function($) {
$.ajax({
type : 'post',
dataType : 'json',
url : upvote_ajax.ajax_url,
data : {
action: 'comment_meta_update_vote',
comment_id: comment_id,
_ajax_nonce: upvote_ajax.nonce
},
success: function( response ) {
if( 'success' == response.type ) {
// console.log('success');
}
else {
// alert( 'Something went wrong, try logging in!' );
}
}
}) })};
Share
Improve this question
asked Aug 14, 2019 at 13:56
J. StocklandJ. Stockland
1156 bronze badges
3
|
1 Answer
Reset to default 0Please try this and inform me what you got in the console. and sure console.log
the type
add_action( 'wp_ajax_comment_meta_update_vote', 'comment_meta_update_vote' );
function comment_meta_update_vote( ) {
check_ajax_referer( 'upvote_nonce' );
$comment_id = $_POST['comment_id'];
$vote_score = get_comment_meta($comment_id , 'vote', true);
$vote_score++;
$updated = update_comment_meta($comment_id , 'vote', $vote_score );
if($updated){
$msg = 'success';
}else{
$msg = 'failed';
}
$response = array('type' => $msg);
wp_json_encode( $response );
die();
}
本文标签: theme developmentAjax Comment UpvotesVotes don39t always register
版权声明:本文标题:theme development - Ajax Comment Upvotes - Votes don't always register 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745241960a2649381.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
update_comment_meta($comment_id , 'vote', $vote_score, true );
should not have a TRUE parameter. Removing that made it work again. – J. Stockland Commented Aug 14, 2019 at 15:16