gpt4 book ai didi

javascript - 如何在 WordPress 中实现 AJAX 评论

转载 作者:行者123 更新时间:2023-11-29 22:04:01 25 4
gpt4 key购买 nike

我正尝试使用本教程在 WordPress 中“ajaxify”评论 Ajaxify WordPress Comments

这是我的 PHP 处理程序:

function ajaxify_comments( $comment_ID, $comment_status ){
if( ! empty( $_SERVER['HTTP_X_REQUESTED_WITH'] ) && strtolower( $_SERVER['HTTP_X_REQUESTED_WITH'] ) == 'xmlhttprequest' ) {
//If AJAX Request Then
switch( $comment_status ) {
case '0':
//notify moderator of unapproved comment
wp_notify_moderator( $comment_ID );
case '1': //Approved comment
echo "success";
$commentdata = &get_comment( $comment_ID, ARRAY_A );
$post = &get_post( $commentdata['comment_post_ID'] );
wp_notify_postauthor( $comment_ID, $commentdata['comment_type'] );
break;
default:
echo "error";
}
exit;
}
}
add_action( 'comment_post', 'ajaxify_comments', 20, 2 );

这是我的脚本:

jQuery('document').ready(function($){
var commentform=$('#commentform'); // find the comment form
commentform.prepend('<div id="comment-status" ></div>'); // add info panel before the form to provide feedback or errors
var statusdiv=$('#comment-status'); // define the infopanel

commentform.submit(function(){
//serialize and store form data in a variable
var formdata=commentform.serialize();
//Add a status message
statusdiv.html('<p>Processing...</p>');
//Extract action URL from commentform
var formurl=commentform.attr('action');
//Post Form with data
$.ajax({
type: 'post',
url: formurl,
data: formdata,
error: function(XMLHttpRequest, textStatus, errorThrown){
statusdiv.html('<p class="ajax-error" >You might have left one of the fields blank, or be posting too quickly</p>');
},
success: function(data, textStatus){
if(data=="success")
statusdiv.html('<p class="ajax-success" >Thanks for your comment. We appreciate your response.</p>');
else
statusdiv.html('<p class="ajax-error" >Please wait a while before posting your next comment</p>');
commentform.find('textarea[name=comment]').val('');
}
});
return false;
});
});

每次我发表评论时,我都会收到:“请稍等片刻,然后再发表您的下一条评论”。希望有人能告诉我我做错了什么?

最佳答案

试试这个:

jQuery('document').ready(function($){
var commentform=$('#commentform'); // find the comment form
commentform.prepend('<div id="comment-status" ></div>'); // add info panel before the form to provide feedback or errors
var statusdiv=$('#comment-status'); // define the infopanel

commentform.submit(function(){
//serialize and store form data in a variable
var formdata=commentform.serialize();
//Add a status message
statusdiv.html('<p>Processing...</p>');
//Extract action URL from commentform
var formurl=commentform.attr('action');
//Post Form with data
$.ajax({
type: 'post',
url: formurl,
data: formdata,
error: function(XMLHttpRequest, textStatus, errorThrown)
{
statusdiv.html('<p class="ajax-error" >You might have left one of the fields blank, or be posting too quickly</p>');
},
success: function(data, textStatus){
if(data == "success" || textStatus == "success"){
statusdiv.html('<p class="ajax-success" >Thanks for your comment. We appreciate your response.</p>');
}else{
statusdiv.html('<p class="ajax-error" >Please wait a while before posting your next comment</p>');
commentform.find('textarea[name=comment]').val('');
}
}
});
return false;
});
});

关于javascript - 如何在 WordPress 中实现 AJAX 评论,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21988153/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com