WordPress AJAX 评论顶踩

原理就是把顶踩的数据储存在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);?>即可,须在评论回调函数中

样式你可以自行修改,我是使用了字体图标

订阅评论
提醒
guest的头像

2 评论
内联反馈
查看所有评论
神经 少侠的头像
管理员
2014-11-12 下午1:27

看起来很靠谱啊

神经 少侠的头像
管理员
2014-11-12 下午1:29

恶霸 ,测试个

2
0
希望看到您的想法,请您发表评论x