wordpress 为主题添加AJAX提交评论功能的php代码
<p><span>首先需要在主题的function.php文件里添加一段函数: </span></p>
<div>
<p>
<span><u>复制代码</u></span></p>
<p>
代码如下:</p>
</div>
<p>
<br>
< ?php <br>
function fail($s) { <br>
header('HTTP/1.0 500 Internal Server Error'); <br>
echo $s; <br>
exit; <br>
} <br>
function ajax_comment(){ <br>
if($_POST['action'] == 'ajax_comment') { <br>
global $wpdb, $db_check; <br>
// Check DB <br>
if(!$wpdb->dbh) { <br>
echo('Our database has issues. Try again later.'); <br>
die(); <br>
} <br>
nocache_headers(); <br>
$comment_post_ID = (int) $_POST['comment_post_ID']; <br>
$status = $wpdb->get_row("SELECT post_status, comment_status FROM $wpdb->posts WHERE ID = '$comment_post_ID'"); <br>
if ( empty($status->comment_status) ) { <br>
do_action('comment_id_not_found', $comment_post_ID); <br>
fail('The post you are trying to comment on does not currently exist in the database.'); <br>
} elseif ( 'closed' == $status->comment_status ) { <br>
do_action('comment_closed', $comment_post_ID); <br>
fail('Sorry, comments are closed for this item.'); <br>
} elseif ( in_array($status->post_status, array('draft', 'pending') ) ) { <br>
do_action('comment_on_draft', $comment_post_ID); <br>
fail('The post you are trying to comment on has not been published.'); <br>
} <br>
$comment_author = trim(strip_tags($_POST['author'])); <br>
$comment_author_email = trim($_POST['email']); <br>
$comment_author_url = trim($_POST['url']); <br>
$comment_content = trim($_POST['comment']); <br>
// If the user is logged in <br>
$user = wp_get_current_user(); <br>
if ( $user->ID ) { <br>
$comment_author = $wpdb->escape($user->display_name); <br>
$comment_author_email = $wpdb->escape($user->user_email); <br>
$comment_author_url = $wpdb->escape($user->user_url); <br>
if ( current_user_can('unfiltered_html') ) { <br>
if ( wp_create_nonce('unfiltered-html-comment_' . $comment_post_ID) != $_POST['_wp_unfiltered_html_comment'] ) { <br>
kses_remove_filters(); // start with a clean slate <br>
kses_init_filters(); // set up the filters <br>
} <br>
} <br>
} else { <br>
if ( get_option('comment_registration') ) <br>
fail('Sorry, you must be logged in to post a comment.'); <br>
} <br>
$comment_type = ''; <br>
if ( get_option('require_name_email') && !$user->ID ) { <br>
if ( 6> strlen($comment_author_email) || '' == $comment_author ) <br>
fail('Sorry: please fill the required fields (name, email).'); <br>
elseif ( !is_email($comment_author_email)) <br>
fail('Sorry: please enter a valid email address.'); <br>
} <br>
if ( '' == $comment_content ) <br>
fail('Sorry: please type a comment.'); <br>
// Simple duplicate check <br>
$dupe = "SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = '$comment_post_ID' AND ( comment_author = '$comment_author' "; <br>
if ( $comment_author_email ) $dupe .= "OR comment_author_email = '$comment_author_email' "; <br>
$dupe .= ") AND comment_content = '$comment_content' LIMIT 1"; <br>
if ( $wpdb->get_var($dupe) ) { <br>
fail('Duplicate comment detected; it looks as though you\'ve already said that!'); <br>
} <br>
$commentdata = compact('comment_post_ID', 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_content', 'comment_type', 'user_ID'); <br>
if( !$user->ID ){ <br>
$result_set = $wpdb->get_results("SELECT display_name, user_email FROM $wpdb->users WHERE display_name = '" . $comment_author . "' OR user_email = '" . $comment_author_email . "'"); <br>
if ($result_set) { <br>
if ($result_set->display_name == $comment_author){ <br>
fail( __('Error: you are not allowed to use the nickname that you entered.if you are the administrator you hava to login to comment.','philna2') ); <br>
} else { <br>
fail( __('Error: you are not allowed to use the email that you entered.if you are the administrator you hava to login to comment.','philna2') ); <br>
} <br>
} <br>
} <br>
$comment_id = wp_new_comment( $commentdata ); <br>
$comment = get_comment($comment_id); <br>
if( !$user->ID ){ <br>
setcookie('comment_author_' . COOKIEHASH, $comment->comment_author, time() + 30000000, COOKIEPATH, COOKIE_DOMAIN); <br>
setcookie('comment_author_email_' . COOKIEHASH, $comment->comment_author_email, time() + 30000000, COOKIEPATH, COOKIE_DOMAIN); <br>
setcookie('comment_author_url_' . COOKIEHASH, clean_url($comment->comment_author_url), time() + 30000000, COOKIEPATH, COOKIE_DOMAIN); <br>
} <br>
@header('Content-type: ' . get_option('html_type') . '; charset=' . get_option('blog_charset')); <br>
?> <br>
//这里需要粘贴你的评论框架代码,不过相关的调用代码有所变化: <br>
//评论ID:$comment->comment_ID <br>
//评论者名字:$comment->comment_author <br>
//判断评论者是否填写了网站地址:$comment->get_comment_author_url <br>
//评论者URL:$comment->comment_author_url <br>
//评论时间:mysql2date(__('F jS, Y'),$comment->comment_date) <br>
//评论者e-mail:$comment->comment_author_email <br>
//评论内容$comment->comment_content <br>
< ?php <br>
die(); <br>
} <br>
} <br>
add_action('init', 'ajax_comment'); <br>
//添加AJAX评论钩子 <br>
?> </p>
<p>
<br><span>如果处理评论内容里的换行符的话,$comment->comment_content需做以下处理: </span></p>
<div>
<p>
<span><u>复制代码</u></span></p>
<p>
代码如下:</p>
</div>
<p>
<br>
< ?php <br>
$aj_order = array("\r\n", "\n", "\r"); <br>
$aj_comment_content = str_replace($aj_order,'<br>
',$comment->comment_content); <br>
echo $aj_comment_content; <br>
?> </p>
<p>
<br><span>用jQuery写提交评论时的脚本,这也是关键的部分: </span></p>
<div>
<p>
<span><u>复制代码</u></span></p>
<p>
代码如下:</p>
</div>
<p>
<br>
jQuery(document).ready(function() { <br>
if (jQuery('#commentform').length) { <br>
jQuery('#commentform').submit(function(){ //ID为 commentform 的表单提交时发生的函数,也就是整个留言输入框 form 的ID。 <br>
var ajaxCommentsURL = window.location.href; <br>
jQuery.ajax({ <br>
url: ajaxCommentsURL, <br>
data: jQuery('#commentform').serialize()+'&action=ajax_comment', <br>
type: 'POST', <br>
beforeSend: function() { <br>
jQuery('#commenterror').hide(); <br>
var submit='<div id="commentload"img/spinner.gif") no-repeat scroll 0 50%;margin: 0 auto;">Submitting Comment...</div>'; //创建名为 submit 的字符串,稍后插入,这里的样式大家自己根据需要定义,那个背景图片自己去下哈。 <br>
var error='<div id="commenterror">Posted comment fail.</div>'; //创建名为 error 的字符串 <br>
jQuery('#comments').after(submit); // 在ID为 comments 的元素后插入刚定义的 submit <br>
jQuery('#comments').after(error); // 同样插入刚定义的 error <br>
jQuery('#commentload').slideDown(); // 让submit 向下滑出 <br>
}, <br>
error: function(request) { //发生错误时 <br>
jQuery('#commentload').hide(); //隐藏 submit <br>
jQuery('#commenterror').show("slow").html(request.responseText); //显示 error <br>
}, <br>
success: function(data) { <br>
jQuery('textarea').each(function(){ <br>
this.value=''; <br>
}); <br>
jQuery('#commenterror').hide().html(); <br>
if (!jQuery('#thecomments').length) { <br>
jQuery('#pinglist').before('<ol id="thecomments"></ol>');} <br>
jQuery('#thecomments').append(data); //向ID为 thecomments 的元素添加数据,也就是整个 ol 或 ul <br>
var new_comment = jQuery('#thecomments li:last').hide(); //让最新添加的数据隐藏 <br>
new_comment.slideDown(1000); //再显示,这里是为了实现滑出的效果,不想要也可以直接显示 <br>
jQuery('#commentform:input').attr('disabled', true); <br>
jQuery('#commentload').slideUp("slow"); <br>
jQuery('#messagebox').slideUp("slow"); //这是针对我模版而加的,因为我模版在没有留言时会有个 nocomment 的元素,我要让添加一条留言后他自动隐藏,要不然会矛盾,呵呵,这个可以自行选择要或不要 <br>
setTimeout(function() { <br>
jQuery('#commentform:input').removeAttr('disabled'); <br>
}, 10000); //这里是设置10秒之后才可以再次留言,自行设置,单位毫秒。 <br>
} <br>
}); <br>
return false; <br>
} ); <br>
}}) </p>
<p>
<br><span>注意: </span><br><span>脚本里的对象ID要对应主题里的评论相关框架的ID。 </span><br><span>后续步骤: </span><br><span>为commentload,commenterror写CSS。 </span><br><span>有待完善的地方: </span><br><span>AJAX提交后的评论序号显示为1。</span></p>
頁:
[1]