WordPress中利用AJAX技术进行评论提交的实现示例


Posted in Javascript onJanuary 12, 2016

一直对 WordPress 的 Ajax 交互研究感兴趣,也一直很关注于这方面的技术,谈到 WordPress Ajax 就不得不谈到评论 Ajax提交,作为一个博客、论坛评论的 Ajax 提交不仅可以改善用户体验,还可以大幅缩减服务器开支,毕竟输出单条评论内容比重新组织输出一个页面要简单的多。 虽说现在访问量一直比较低,不存在服务器压力的问题,但一向注重用户体验的我,当然不能放弃这么一个提升用户体验的机会。今天抽了一下午的空,把这个主题的 Ajax 评论提交初步完成了。

直接开门见山,直接上代码:(原理及思路在最后)
根据自己主题不同结构,以下代码请自行调整。

WordPress Ajax 提交评论 PHP 代码
在主题 function.php 文件中加入如下部分。

//以下大部分代码出自 yinheli 经由该部分代码,排除部分错误、优化精简得出以下代码。
//yinheli博客不做了,所以这里就不给链接了。
//Edited by XiangZi DEC.17TH 2011
function 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 评论插件,
一些高手也只能在优化自己博客之余,把思路和部分通用核心代码做一下公布,
所以想要实现一些炫酷的功能要不有高人帮你,
要不你就只能好好学代码,期待有一日能够厚积薄发了。
效果请自行提交评论验证。

Javascript 相关文章推荐
用JavaScript事件串连执行多个处理过程的方法
Mar 09 Javascript
Jquery数独游戏解析(一)-页面布局
Nov 05 Javascript
基于JQuery的一句话搞定手风琴菜单
Sep 14 Javascript
Javascript实现获取及设置光标位置的方法
Jul 21 Javascript
AngularJS入门教程之AngularJS表达式
Apr 18 Javascript
javascript 用函数实现继承详解
May 28 Javascript
vue.js实现价格格式化的方法
May 23 Javascript
jQuery实现点击下拉框中的值累加到文本框中的方法示例
Oct 28 jQuery
node中IO以及定时器优先级详解
May 10 Javascript
express中static中间件的具体使用方法
Oct 17 Javascript
js实现百度淘宝搜索功能
Feb 17 Javascript
Vue 3自定义指令开发的相关总结
Jan 29 Vue.js
基于JavaScript实现div层跟随滚动条滑动
Jan 12 #Javascript
JavaScript继承模式粗探
Jan 12 #Javascript
轻松实现Bootstrap图片轮播
Apr 20 #Javascript
探析浏览器执行JavaScript脚本加载与代码执行顺序
Jan 12 #Javascript
学习JavaScript设计模式之策略模式
Jan 12 #Javascript
基于jQuery1.9版本如何判断浏览器版本类型
Jan 12 #Javascript
jQuery版本升级踩坑大全
Jan 12 #Javascript
You might like
yii去掉必填项中星号的方法
2015/12/28 PHP
PHP实现基于mysqli的Model基类完整实例
2016/04/08 PHP
IE 条件注释详解总结(附实例代码)
2009/08/29 Javascript
fireworks菜单生成器mm_menu.js在 IE 7.0 显示问题的解决方法
2009/10/20 Javascript
菜鸟javascript基础整理1
2010/12/06 Javascript
js 中{},[]中括号,大括号使用详解
2011/05/12 Javascript
jQuery查询数据返回object和字符串影响原因是什么
2013/08/09 Javascript
js日期联动示例
2014/05/02 Javascript
基于jquery步骤进度条源码分享
2015/11/12 Javascript
使用JQuery中的trim()方法去掉前后空格
2016/09/16 Javascript
JS冒泡事件与事件捕获实例详解
2016/11/25 Javascript
canvas实现粒子时钟效果
2017/02/06 Javascript
vue项目中jsonp跨域获取qq音乐首页推荐问题
2018/05/30 Javascript
微信小程序实现获取小程序码和二维码java接口开发
2019/03/29 Javascript
[48:29]2018DOTA2亚洲邀请赛3月30日 小组赛A组 LGD VS KG
2018/03/31 DOTA
在Docker上开始部署Python应用的教程
2015/04/17 Python
深入解析Python中的lambda表达式的用法
2015/08/28 Python
python django使用haystack:全文检索的框架(实例讲解)
2017/09/27 Python
pandas 对series和dataframe进行排序的实例
2018/06/09 Python
python 不以科学计数法输出的方法
2018/07/16 Python
OpenCV+Python识别车牌和字符分割的实现
2019/01/31 Python
对python For 循环的三种遍历方式解析
2019/02/01 Python
Python中使用__new__实现单例模式并解析
2019/06/25 Python
python定时任务 sched模块用法实例
2019/11/04 Python
python名片管理系统开发
2020/06/18 Python
浅谈对python中if、elif、else的误解
2020/08/20 Python
Css3实现无缝滚动防抖
2020/09/14 HTML / CSS
Html5 canvas画图白板踩坑
2020/06/01 HTML / CSS
美国高端寝具品牌:Coyuchi
2017/02/08 全球购物
工作会议欢迎词
2014/01/16 职场文书
违反学校规定检讨书
2014/01/18 职场文书
关于旅游的活动方案
2014/08/15 职场文书
教师四风问题整改措施
2014/09/25 职场文书
倡议书范文大全
2015/04/28 职场文书
《乘法分配律》教学反思
2016/02/24 职场文书
确保减税降费落地生根,用实实在在措施
2019/07/19 职场文书