Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124

原理就是把顶踩的数据储存在comment_meta中,非常方便
很久之前我曾经写过一个非插件实现评论投票,当时是从一个很老的插件中拔出来的,方法很弱,而且还新建了数据库表项,当时是自己写不出来,只能研究别人的,现在终于可以写出自己的了。
原理就是把顶踩的数据储存在comment_meta中,非常方便~
下面是ajax响应函数,加到functions.php中
add_action('wp_ajax_nopriv_do_comment_rate', 'do_comment_rate');
add_action('wp_ajax_do_comment_rate', 'do_comment_rate');
function do_comment_rate(){
if (!isset($_POST["comment_id"]) || !isset($_POST["event"])) {
$data = array("status"=>500,"data"=>'?');
echo json_encode($data);
} else {
$comment_id = $_POST["comment_id"];
$event = $_POST["event"];
$expire = time() + 99999999;
$domain = ($_SERVER['HTTP_HOST'] != 'localhost') ? $_SERVER['HTTP_HOST'] : false; // make cookies work with localhost
setcookie('comment_rated_'.$comment_id,$comment_id,$expire,'/',$domain,false);
$_comment_up = get_comment_meta($comment_id,'_comment_up',true);
$_comment_down = get_comment_meta($comment_id,'_comment_down',true);
if ($event == "up") {
if (!$_comment_up) {
update_comment_meta($comment_id, '_comment_up', 1);
} else {
update_comment_meta($comment_id, '_comment_up', ($_comment_up + 1));
}
} else {
if (!$_comment_down || $_comment_down == '' || !is_numeric($_comment_down)) {
update_comment_meta($comment_id, '_comment_down', 1);
} else {
update_comment_meta($comment_id, '_comment_down', ($_comment_down + 1));
}
}
$data = array();
$_comment_up = get_comment_meta($comment_id,'_comment_up',true);
$_comment_down = get_comment_meta($comment_id,'_comment_down',true);
$data = array("status"=>200,"data"=>array("event"=>$event,"_comment_up"=>$_comment_up,"_comment_down"=>$_comment_down));
echo json_encode($data);
}
die;
}
这里是显示投票选项的函数,也是加到functions.php中
function comment_rate($comment_ID = 0,$echo = true){
$_comment_up = get_comment_meta($comment_ID,'_comment_up',true) ? get_comment_meta($comment_ID,'_comment_up',true) : 0;
$_comment_down = get_comment_meta($comment_ID,'_comment_down',true) ? get_comment_meta($comment_ID,'_comment_down',true) : 0 ;
$done = "";
if (isset($_COOKIE['comment_rated_'.$comment_ID])) $done = " rated";
$content = '<span class="comment--like'.$done.'" data-commentid="'.$comment_ID.'"><a href="javascript:;" data-event="up"><i class="iconfont icon-arrowup"></i><em class="count">'.$_comment_up.'</em></a><a href="javascript:;" data-event="down"><i class="iconfont icon-arrowdown"></i><em class="count">'.$_comment_down.'</em></a></span>';
if ($echo) {
echo $content;
} else {
return $content;
}
}
删除评论时也删除相关信息,也是加到functions.php中
add_action('delete_comment', 'delete_comment_ratings_fields');
function delete_comment_ratings_fields($comment_ID) {
global $wpdb;
delete_comment_meta($comment_ID, '_comment_up');
delete_comment_meta($comment_ID, '_comment_down');
}
js代码
jQuery(document).on("click", ".comment--like a",
function() {
var $this = jQuery(this);
var comment_id = $this.parent().data("commentid");
var event = $this.data("event");
var count = $this.children(".count");
if ($this.parent().hasClass("rated")) {
alert("you've rated");
return false;
} else {
var ajax_data = {
action: "do_comment_rate",
comment_id: comment_id,
event: event
};
jQuery.ajax({
url: '/wp-admin/admin-ajax.php',
type: "POST",
data: ajax_data,
dataType: "json",
success: function(data) {
if (data.status == 200) {
if (event == "up") {
count.html(data.data._comment_up);
} else {
count.html(data.data._comment_down);
}
$this.parent().addClass("rated");
} else {
console.log(data.data)
}
}
});
}
return false;
});
在你想显示的地方加入<?php comment_rate(get_comment_ID(),true);?>即可,须在评论回调函数中
样式你可以自行修改,我是使用了字体图标
看起来很靠谱啊
恶霸 ,测试个