一直对 wordpress 的 ajax 交互研究感兴趣,也一直很关注于这方面的技术,谈到 wordpress ajax 就不得不谈到评论 ajax提交,作为一个博客、论坛评论的 ajax 提交不仅可以改善用户体验,还可以大幅缩减服务器开支,毕竟输出单条评论内容比重新组织输出一个页面要简单的多。 虽说现在访问量一直比较低,不存在服务器压力的问题,但一向注重用户体验的我,当然不能放弃这么一个提升用户体验的机会。今天抽了一下午的空,把这个主题的 ajax 评论提交初步完成了。
直接开门见山,直接上代码:(原理及思路在最后)
根据自己主题不同结构,以下代码请自行调整。
wordpress ajax 提交评论 php 代码
在主题 function.php 文件中加入如下部分。
//以下大部分代码出自 yinheli 经由该部分代码,排除部分错误、优化精简得出以下代码。//yinheli博客不做了,所以这里就不给链接了。//edited by xiangzi dec.17th 2011function fail($s) {//虚拟错误头部分 header('http/1.0 500 internal server error'); echo $s; exit;}function ajax_post_comment_slow (){ fail('用不用说这么快?想好了再说!');}//评论太快输出代码。add_filter('comment_flood_trigger','ajax_post_comment_slow', 0);//挂一个评论太快,返回内容的钩子function ajax_comment(){// ajax php 响应部分代码if($_post['action'] == 'ajax_comment') { global $wpdb, $db_check; // check db if(!$wpdb->dbh) { echo('our database has issues. try again later.'); die(); } nocache_headers();$comment_post_id = (int) $_post['comment_post_id']; $status = $wpdb->get_row(select post_status, comment_status from $wpdb->posts where id = '$comment_post_id');if ( empty($status->comment_status) ) {//这一套判断貌似抄的 wp 源代码 。详见:include/comment.php do_action('comment_id_not_found', $comment_post_id); fail('the post you are trying to comment on does not currently exist in the database.');} elseif ( 'closed' == $status->comment_status ) { do_action('comment_closed', $comment_post_id);; fail('sorry, comments are closed for this item.');} elseif ( in_array($status->post_status, array('draft', 'pending') ) ) { do_action('comment_on_draft', $comment_post_id); fail('the post you are trying to comment on has not been published.');}$comment_author = trim(strip_tags($_post['author']));$comment_author_email = trim($_post['email']);$comment_author_url = trim($_post['url']);$comment_content = trim($_post['comment']);// if the user is logged in$user = wp_get_current_user();if ( $user->id ) { $comment_author = $wpdb->escape($user->display_name); $comment_author_email = $wpdb->escape($user->user_email); $comment_author_url = $wpdb->escape($user->user_url); if ( current_user_can('unfiltered_html') ) { if ( wp_create_nonce('unfiltered-html-comment_' . $comment_post_id) != $_post['_wp_unfiltered_html_comment'] ) { kses_remove_filters(); // start with a clean slate kses_init_filters(); // set up the filters } }} else { if ( get_option('comment_registration') ) fail('火星人?注册个?');}$comment_type = '';if ( get_option('require_name_email') && !$user->id ) { if ( 6> strlen($comment_author_email) || '' == $comment_author ) fail('oopps,名字[name]或邮箱[email]不对。'); elseif ( !is_email($comment_author_email)) fail('oopps,邮箱地址[email]不对。');}if ( '' == $comment_content ) fail('是不是应该写点什么再提交?');// simple duplicate check$dupe = select comment_id from $wpdb->comments where comment_post_id = '$comment_post_id' and ( comment_author = '$comment_author' ;if ( $comment_author_email ) $dupe .= or comment_author_email = '$comment_author_email' ;$dupe .= ) and comment_content = '$comment_content' limit 1;if ( $wpdb->get_var($dupe) ) { fail('评论重复了!有木有!');}$commentdata = compact('comment_post_id', 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_content', 'comment_type', 'user_id');if( !$user->id ){ $result_set = $wpdb->get_results(select display_name, user_email from $wpdb->users where display_name = ' . $comment_author . ' or user_email = ' . $comment_author_email . '); if ($result_set) { if ($result_set[0]->display_name == $comment_author){ fail('博主你也敢冒充?'); } else { fail('博主你也敢冒充?'); } }}$comment_id = wp_new_comment( $commentdata );$comment = get_comment($comment_id); if( !$user->id ){ setcookie('comment_author_' . cookiehash, $comment->comment_author, time() + 30000000, cookiepath, cookie_domain); setcookie('comment_author_email_' . cookiehash, $comment->comment_author_email, time() + 30000000, cookiepath, cookie_domain); setcookie('comment_author_url_' . cookiehash, clean_url($comment->comment_author_url), time() + 30000000, cookiepath, cookie_domain);}@header('content-type: ' . get_option('html_type') . '; charset=' . get_option('blog_charset')); xz_comment($comment, null);//这是我的调用评论函数,换成你的函数名。 die();}}add_action('init', 'ajax_comment');
javascript 中代码
注意:以下代码需要 jquery 框架支援。
javascript onload 代码中加入以下部分。
if (jquery('#commentform').length) { jquery('#commentform').submit(function(){ // 截获提交动作//id为 commentform 的表单提交时发生的函数,也就是整个留言输入框 form 的id。 var ajaxcommentsurl = window.location.href; jquery.ajax({ url: ajaxcommentsurl, data: jquery('#commentform').serialize()+'&action=ajax_comment', type: 'post', beforesend: function() { jquery('#commenterror').hide(); jquery('#commentload').fadein(); }, error: function(request) { //发生错误时 jquery('#commenterror').html(request.responsetext); jquery('#commentload').hide(); //隐藏 submit jquery('#commenterror').fadein(); //显示 error }, success: function(data) { jquery('textarea').each(function(){ this.value=''; }); jquery('#commenterror').fadeout(); if(jquery(.commentlist li.comment).first().length != 0){jquery(.commentlist li.comment).first().before(data)} else {jquery(ol.commentlist).append(data)} jquery(.commentlist li.comment).first().hide(0,function(){$(this).slidedown(1000)}); jquery('#cmt-submit').attr('disabled', true).css({background-color:#6c6c6c,color:#e0e0e0}); jquery('#commentload').fadeout(1600); settimeout(function() { jquery('#cmt-submit').removeattr('disabled').css({background-color:#0086c5,color:#ffffff}); },3000); } }); return false; } );}
注:代码仍有改进需求,因为没有时间,所以就没有再进化。
css 代码
css 随意部分添加。
#commentload,#commenterror{ display: none; margin: 5px 0 0 0; color:#d29a04; float: left; font-size:16px; padding:0 0 0 20px;}#commentload{ background: url(img/loading.gif) no-repeat bottom left ;}#commenterror{ background: url(img/error.png) no-repeat bottom left ;}
原理、思路
原理:
javascript 提交数据
php响应并输出结果
javascript 得到结果并显示
思路:
点击提交按钮后,javascript 截获提交动作
截获提交的各项数据(name、email、web、comment-text)
利用 javascript jquery 模拟浏览器提交post(name、email、web、comment-text)请求之wordpress
function.php 文件中构造一个接受请求的函数,即本列中ajax_comment函数
如果请求无错误,输出正确结果
如果请求有错误,输出错误结果
javascript 获得正确结果,动态添加到评论列表中
javascript 获得错误结果,动态添加到提交提示栏
改进
样式方面,我确实没什么美感,所以正在学习中。
提交按钮在点击至获得返回结果后3秒的时间里应该都是变灰失效状态,这一点之前因为在本机测试,提交瞬间完成没有注意到,远程测试的时候发现了,但要改的话还要进行测试,时间太紧就不改了,有机会再改进一下。
总结
因为 wordpress 主题中评论样式的自由性、多样性,所以貌似至今一直没有一款通用性的ajax 评论插件,
一些高手也只能在优化自己博客之余,把思路和部分通用核心代码做一下公布,
所以想要实现一些炫酷的功能要不有高人帮你,
要不你就只能好好学代码,期待有一日能够厚积薄发了。
效果请自行提交评论验证。