尝试以下操作:http://wordpress.org/plugins/wp-ajaxify-comments/
或(手动)
将以下代码行添加到主题的函数中。php文件
add_action(\'init\', \'wdp_ajaxcomments_load_js\', 10);
function wdp_ajaxcomments_load_js(){
wp_enqueue_script(\'ajaxValidate\', get_stylesheet_directory_uri().\'/wdp-ajaxed-comments/js/jquery.validate.min.js\', array(\'jquery\'), \'1.5.5\');
wp_enqueue_script(\'ajaxcomments\', get_stylesheet_directory_uri().\'/wdp-ajaxed-comments/js/ajax-comments.js\', array(\'jquery\', \'ajaxValidate\'), \'1.1\');
}
add_action(\'comment_post\', \'wdp_ajaxcomments_stop_for_ajax\',20, 2);
function wdp_ajaxcomments_stop_for_ajax($comment_ID, $comment_status){
if(!emptyempty($_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\']); //Notify post author of comment
if ( get_option(\'comments_notify\') && $commentdata[\'comment_approved\'] && $post->post_author != $commentdata[\'user_ID\'] )
wp_notify_postauthor($comment_ID, $commentdata[\'comment_type\']);
break;
default:
echo "error";
}
exit;
}
}
您需要添加两个JavaScript文件jquery。验证min.js和ajax注释。js位于主题的js目录中。
http://bassistance.de/jquery-plugins/jquery-plugin-validation/以及ajax注释。js是:
jQuery(\'document\').ready(function($){
var commentform=$(\'form[action$=wp-comments-post.php]\');
commentform.prepend(\'<div id="wdpajax-info" ></div>\');
var infodiv=$(\'#wdpajax-info\');
commentform.validate({
submitHandler: function(form){
//serialize and store form data in a variable
var formdata=commentform.serialize();
//Add a status message
infodiv.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){
infodiv.html(\'<p class="wdpajax-error" >You might have left one of the fields blank.</p>\');
},
success: function(data, textStatus){
if(data=="success")
infodiv.html(\'<p class="wdpajax-success" >Thanks for your comment. We appreciate your response.</p>\');
else
infodiv.html(\'<p class="wdpajax-error" >Error in processing your form.</p>\');
commentform.find(\'textarea[name=comment]\').val(\'\');
}
});
}
});
});
自定义消息的样式
.wdpajax-error{
border:1px solid #f9d9c9;
padding:5px;
color:#ff3311;
}
.wdpajax-success{
border:1px solid #339933;
padding:5px;
color:#339933;
}
label.error{
float:none !important;
padding-left:5px;
color:#ff3311;
}
打开主题的注释。php并向注释表单输入字段添加一些CSS类,如下所述:要注释作者姓名输入,请添加class=“required”以注释作者电子邮件输入,添加class=“required email”以注释作者URL输入,将class=“URL”添加到注释文本区,添加class=“required”