首先需要在主题的function.php文件里添加一段函数:

复制代码代码如下:

< ?php

function fail($s) {

header('HTTP/1.0 500 Internal Server Error');

echo $s;

exit;

}

function ajax_comment(){

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) ) {

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('Sorry, you must be logged in to post a comment.');

}

$comment_type = '';

if ( get_option('require_name_email') && !$user->ID ) {

if ( 6> strlen($comment_author_email) || '' == $comment_author )

fail('Sorry: please fill the required fields (name, email).');

elseif ( !is_email($comment_author_email))

fail('Sorry: please enter a valid email address.');

}

if ( '' == $comment_content )

fail('Sorry: please type a comment.');

// 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('Duplicate comment detected; it looks as though you\'ve already said that!');

}

$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( __('Error: you are not allowed to use the nickname that you entered.if you are the administrator you hava to login to comment.','philna2') );

} else {

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') );

}

}

}

$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'));

?>

//这里需要粘贴你的评论框架代码,不过相关的调用代码有所变化:

//评论ID:$comment->comment_ID

//评论者名字:$comment->comment_author

//判断评论者是否填写了网站地址:$comment->get_comment_author_url

//评论者URL:$comment->comment_author_url

//评论时间:mysql2date(__('F jS, Y'),$comment->comment_date)

//评论者e-mail:$comment->comment_author_email

//评论内容$comment->comment_content

< ?php

die();

}

}

add_action('init', 'ajax_comment');

//添加AJAX评论钩子

?>

如果处理评论内容里的换行符的话,$comment->comment_content需做以下处理:

复制代码代码如下:

< ?php

$aj_order = array("\r\n", "\n", "\r");

$aj_comment_content = str_replace($aj_order,'

',$comment->comment_content);

echo $aj_comment_content;

?>

用jQuery写提交评论时的脚本,这也是关键的部分:

复制代码代码如下:

jQuery(document).ready(function() {

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();

var submit='

Submitting Comment...

'; //创建名为 submit 的字符串,稍后插入,这里的样式大家自己根据需要定义,那个背景图片自己去下哈。

var error='

Posted comment fail.

'; //创建名为 error 的字符串

jQuery('#comments').after(submit); // 在ID为 comments 的元素后插入刚定义的 submit

jQuery('#comments').after(error); // 同样插入刚定义的 error

jQuery('#commentload').slideDown(); // 让submit 向下滑出

},

error: function(request) { //发生错误时

jQuery('#commentload').hide(); //隐藏 submit

jQuery('#commenterror').show("slow").html(request.responseText); //显示 error

},

success: function(data) {

jQuery('textarea').each(function(){

this.value='';

});

jQuery('#commenterror').hide().html();

if (!jQuery('#thecomments').length) {

jQuery('#pinglist').before('

');}

jQuery('#thecomments').append(data); //向ID为 thecomments 的元素添加数据,也就是整个 ol 或 ul

var new_comment = jQuery('#thecomments li:last').hide(); //让最新添加的数据隐藏

new_comment.slideDown(1000); //再显示,这里是为了实现滑出的效果,不想要也可以直接显示

jQuery('#commentform:input').attr('disabled', true);

jQuery('#commentload').slideUp("slow");

jQuery('#messagebox').slideUp("slow"); //这是针对我模版而加的,因为我模版在没有留言时会有个 nocomment 的元素,我要让添加一条留言后他自动隐藏,要不然会矛盾,呵呵,这个可以自行选择要或不要

setTimeout(function() {

jQuery('#commentform:input').removeAttr('disabled');

}, 10000); //这里是设置10秒之后才可以再次留言,自行设置,单位毫秒。

}

});

return false;

} );

}})

注意:

脚本里的对象ID要对应主题里的评论相关框架的ID。

后续步骤:

为commentload,commenterror写CSS。

有待完善的地方:

AJAX提交后的评论序号显示为1。

wordpressPHP实现ajax评论,AJAX_wordpress 为主题添加AJAX提交评论功能的php代码,首先需要在主题的function.php文 - phpStudy...相关推荐

  1. php增加vip等级设置,xiu主题添加vip等级评论样式

    先来张效果图吧: 这种效果应该有部分博主已经添加了,但 xiu 主题如何添加呢? 首先需要在主题 functions.php 添加下面一串函数 //获取访客 VIP 样式 function get_a ...

  2. php怎么设置顶部图标,为WordPress主题添加顶部自定义图片功能

    在WordPress主题中,头部(header)的自定义一般来说就是能让用户在后台能够自行更改主题默认显示的图片或者标题的大小颜色这些功能.而为一个主题添加上这些后台选项并不难,在WordPress较 ...

  3. hexo评论_hexo博客之next主题添加畅言评论

    tips: 接下去会在github写博客,简书不再更新和修改文章,欢迎大家逛逛我的新博客博客地址,我会尽量用更容易理解的方式写好每一篇博客,大家一起学习交流

  4. 给hexo主题添加背景及更改字体颜色(next的Mist主题)

    最近新搭的个人博客:qsang.xin 采用的是hexo + Github,next主题,自己动手更改了一下 其实要更改某一部分,只需要F12找到元素然后找到相应元素就可以啦~ 进入正题 更改背景 找 ...

  5. Hexo + yilia 主题 +githubpages博客添加友言评论功能

    前言 Hexo博客的Yilia主题中评论系统只提供了畅言.网易云跟帖.多说和Disqus. 由于多说评论.网易云跟帖已经关闭系统,畅言需要域名备案,而github是国外的,没有备案,所以尝试了其他第三 ...

  6. WordPress主题添加鼠标点击特效

    关于WordPress主题添加鼠标点击特效方法以及代码使用.看着花里胡哨的,其实实现很简单.找到相对应的文件位置,然后复制粘贴,最后保存运行即可.为了方便新手我尽可能详细些,使用方法:打开宝塔Linu ...

  7. [Debugging]分析博客园提交评论的校验规则

    今天偶然发现,在博客园同时打开多篇文章,如果短时间内分别在不同文章里提交相同内容的评论,就会提示"该评论已发表过!".一开始感觉这应该是个bug,毕竟在同一篇文章里,并不存在相同的 ...

  8. wordpress主题ajax,为自制WordPress主题/插件的后台设置页面添加ajax支持

    本文目录 [隐藏] 1PHP部分 1.1安全第一 1.2定义一些用得上的常量 1.3I18n=国际化支持 1.4添加菜单项 1.5美化下菜单项前面的icon 1.6设置页面/后台的HTML结构 1.7 ...

  9. hexo yilia主题添加评论系统详细教程

    文章目录 序言 添加相关代码 1. config.yml 2. yilia\layout\_partial\article.ejs 3.valine.ejs 安装评论 序言 其实网上关于Hexo的博客 ...

最新文章

  1. idea创建maven的web项目
  2. (转载)文件系统与数据库系统的区别
  3. 用python找对象_还在单身的你 Python教你如何脱单
  4. c语言整型的最大范围,整型变量范围最大到32750?
  5. python3 ocr_python3 ocr 识别图片文字(CSDN验证码90%通过)
  6. Facebook广告营销的6个方法经验分享
  7. sunplus 8202v iop源代码阅读笔记——1
  8. php中fread用法,phpfread函数与fread函数用法_PHP教程
  9. 数据挖掘导论阅读笔记第一章:绪论
  10. EasyRecovery2022版支持电脑, 硬盘, U盘, 内存卡, 回收站等设备数据恢复
  11. 做在线交易你必须知道的关于支付的知识
  12. uniny 物体运动到一个点停止_unity控制运动
  13. 如何屏蔽知乎网页中的热搜
  14. 破解网易云js加密,爬虫获取网易云评论
  15. Ubuntu下Linux系统部署fisco时bash操作报错权限不足(permission denied) failed to run command ‘........‘
  16. 会声会影2021软件下载如何制作视频剪辑制作教学
  17. 【算法】求解幂集问题
  18. 程序运行时的内存空间分布
  19. jaybird+x3+android,小米首款运动耳机
  20. 蓝豹子2.8程序设计题

热门文章

  1. yolo loss 将图像标注的真实事坐标转换到anchor坐标
  2. opencv 车牌切割
  3. word2vect 输出是什么
  4. Leetcode 136.只出现一次的数字 (每日一题 20210714)
  5. Leetcode每日必刷题库第2题,如何实现两数相加?
  6. 用Tableau画Voronoi Treemap
  7. 强化学习(十二) Dueling DQN
  8. SVM在R语言中的使用
  9. php怎么从文件中读取数据库连接,PHP连接 读取 写入mysql数据库的方法 附常用代码...
  10. Matplotlib实例教程(二)饼状图