Enter your keyword

Blog

Comment Like and Dislike Function in Wordpess

// Create Table in your Databse SQL
CREATE TABLE custom_comment_likes(
    id INT NOT NULL AUTO_INCREMENT,
    comment_id INT NOT NULL,
    ip_address VARCHAR (255),
    status INT NOT NULL,
    PRIMARY KEY (id)
);
function dsweb_comment_like_dislike(){
    if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
        $ip = $_SERVER['HTTP_CLIENT_IP'];
    } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
    } else {
        $ip = $_SERVER['REMOTE_ADDR'];
    }
    global $wpdb;
    $like_count = $wpdb->get_var( "SELECT * FROM custom_comment_likes WHERE `ip_address` = '".$ip."' AND `comment_id` = '".$_POST[id]."'");
    $count = $wpdb->num_rows;
    if(!$count > 0){
        $wpdb->insert( 'custom_comment_likes',  array(  'ip_address' => $ip, 'comment_id' => $_POST[id] , 'status' => $_POST[status]),  array( '%s', '%d' ,'%d') );
    }else{
        $wpdb->update('custom_comment_likes', array( 'status' => $_POST[status]), array( 'comment_id' => $_POST[id] ,'ip_address' => $ip), array('%d' ),array( '%d','%s' ) );
    }
    $like_query = $wpdb->get_results("SELECT * FROM custom_comment_likes WHERE `comment_id` = '".$_POST[id]."' AND `status` = '1'");
    $like = $wpdb->num_rows;
    $dislike_query = $wpdb->get_results("SELECT * FROM custom_comment_likes WHERE `comment_id` = '".$_POST[id]."' AND `status` = '0'");
    $dislike = $wpdb->num_rows;
    $arg['success']['like'] = $like;
    $arg['success']['dislike'] = $dislike;
    return $arg;
}

No Comments

Add your review

Your email address will not be published.

* Feilds are Required.