جستجو برای:
سبد خرید 0
  • مجله آموزشی برنامه نویسی
  • دوره ها
    • حساب کاربری
    • سبد خرید
  • بلاگ
 

ورود

رمز عبور را فراموش کرده اید؟

هنوز عضو نشده اید؟ عضویت در سایت
  • ۰۹۱۳۶۵۲۳۵۹۵
  • support@ariapad.com
perm_identity
سبد خرید 0
  • مجله آموزشی برنامه نویسی
  • دوره ها
  • بلاگ
0
آریاپاد
  • مجله آموزشی برنامه نویسی
  • آموزش های متنی
    • آموزش زبان برنامه نویسی PHP
      • آموزش PHP از مبتدی تا پیشرفته
      • آموزش فریم ورک لاراول
    • وردپرس
      • آموزش ساخت پوسته وردپرس
      • افزونه های مورد نیاز و حرفه ای وردپرس
      • افزونه های وردپرس آریاپاد
      • بهینه سازی سایت های وردپرسی
    • سرور
      • آموزش هاست و سرور
      • امنیت سرور مجازی و اختصاصی لینوکس
    • سئو و مطالب آزاد
      • #ویدئوها
      • #پادکست(صوتی)
      • دوره آموزشی جامع سئو
      • نوشته های آزاد
  • دوره های آموزشی ویدئویی
  • منو کاربری
    • کیف پول من
    • فراموشی گذرواژه
    • جزئیات حساب
    • آدرس‌ها
    • دانلودها
    • علاقه مندی ها
    • دوره های خریداری شده
    • سفارش‌ها
    • حساب کاربری
    • سبد خرید
    • پرداخت
  • صفحات
    • درباره ما
    • جداول قیمت گذاری
    • اساتید
    • نمونه کارها
ورود و ثبت نام

وبلاگ

آریاپاد بلاگ وردپرس آموزش ساخت پوسته وردپرس اضافه کردن دکمه لایک به پست ها و نظرات وردپرس بدون پلاگین

اضافه کردن دکمه لایک به پست ها و نظرات وردپرس بدون پلاگین

access_time1396/07/09
perm_identity ارسال شده توسط شمشاد امیری خراسانی
folder_open آموزش ساخت پوسته وردپرس
visibility 47 بازدید


ابتدا کد زیر را به فایل functions.php پوسته اضافه کنید :

//like post Ariapadweb 
/*
Name:  WordPress Post Like System
Description:  A simple and efficient post like system for WordPress.
Version:      0.5.2
Author:       Jon Masterson
Author URI:   http://jonmasterson.com/
License:
Copyright (C) 2015 Jon Masterson
*/

/**
 * Register the stylesheets for the public-facing side of the site.
 * @since    0.5
 */
add_action('wp_enqueue_scripts', 'sl_enqueue_scripts');
function sl_enqueue_scripts()
{
    wp_enqueue_script('simple-likes-public-js', get_template_directory_uri() . '/js/simple-likes-public.js', array('jquery'), '0.5', false);
    wp_localize_script('simple-likes-public-js', 'simpleLikes', array(
        'ajaxurl' => admin_url('admin-ajax.php'),
        'like' => __('محبوب', 'YourThemeTextDomain'),
        'unlike' => __('متنفر', 'YourThemeTextDomain')
    ));
}

/**
 * Processes like/unlike
 * @since    0.5
 */
add_action('wp_ajax_nopriv_process_simple_like', 'process_simple_like');
add_action('wp_ajax_process_simple_like', 'process_simple_like');
function process_simple_like()
{
    // Security
    $nonce = isset($_REQUEST['nonce']) ? sanitize_text_field($_REQUEST['nonce']) : 0;
    if (!wp_verify_nonce($nonce, 'simple-likes-nonce')) {
        exit(__('Not permitted', 'YourThemeTextDomain'));
    }
    // Test if javascript is disabled
    $disabled = (isset($_REQUEST['disabled']) && $_REQUEST['disabled'] == true) ? true : false;
    // Test if this is a comment
    $is_comment = (isset($_REQUEST['is_comment']) && $_REQUEST['is_comment'] == 1) ? 1 : 0;
    // Base variables
    $post_id = (isset($_REQUEST['post_id']) && is_numeric($_REQUEST['post_id'])) ? $_REQUEST['post_id'] : '';
    $result = array();
    $post_users = NULL;
    $like_count = 0;
    // Get plugin options
    if ($post_id != '') {
        $count = ($is_comment == 1) ? get_comment_meta($post_id, "_comment_like_count", true) : get_post_meta($post_id, "_post_like_count", true); // like count
        $count = (isset($count) && is_numeric($count)) ? $count : 0;
        if (!already_liked($post_id, $is_comment)) { // Like the post
            if (is_user_logged_in()) { // user is logged in
                $user_id = get_current_user_id();
                $post_users = post_user_likes($user_id, $post_id, $is_comment);
                if ($is_comment == 1) {
                    // Update User & Comment
                    $user_like_count = get_user_option("_comment_like_count", $user_id);
                    $user_like_count = (isset($user_like_count) && is_numeric($user_like_count)) ? $user_like_count : 0;
                    update_user_option($user_id, "_comment_like_count", ++$user_like_count);
                    if ($post_users) {
                        update_comment_meta($post_id, "_user_comment_liked", $post_users);
                    }
                } else {
                    // Update User & Post
                    $user_like_count = get_user_option("_user_like_count", $user_id);
                    $user_like_count = (isset($user_like_count) && is_numeric($user_like_count)) ? $user_like_count : 0;
                    update_user_option($user_id, "_user_like_count", ++$user_like_count);
                    if ($post_users) {
                        update_post_meta($post_id, "_user_liked", $post_users);
                    }
                }
            } else { // user is anonymous
                $user_ip = sl_get_ip();
                $post_users = post_ip_likes($user_ip, $post_id, $is_comment);
                // Update Post
                if ($post_users) {
                    if ($is_comment == 1) {
                        update_comment_meta($post_id, "_user_comment_IP", $post_users);
                    } else {
                        update_post_meta($post_id, "_user_IP", $post_users);
                    }
                }
            }
            $like_count = ++$count;
            $response['status'] = "liked";
            $response['icon'] = get_liked_icon();
        } else { // Unlike the post
            if (is_user_logged_in()) { // user is logged in
                $user_id = get_current_user_id();
                $post_users = post_user_likes($user_id, $post_id, $is_comment);
                // Update User
                if ($is_comment == 1) {
                    $user_like_count = get_user_option("_comment_like_count", $user_id);
                    $user_like_count = (isset($user_like_count) && is_numeric($user_like_count)) ? $user_like_count : 0;
                    if ($user_like_count > 0) {
                        update_user_option($user_id, "_comment_like_count", --$user_like_count);
                    }
                } else {
                    $user_like_count = get_user_option("_user_like_count", $user_id);
                    $user_like_count = (isset($user_like_count) && is_numeric($user_like_count)) ? $user_like_count : 0;
                    if ($user_like_count > 0) {
                        update_user_option($user_id, '_user_like_count', --$user_like_count);
                    }
                }
                // Update Post
                if ($post_users) {
                    $uid_key = array_search($user_id, $post_users);
                    unset($post_users[$uid_key]);
                    if ($is_comment == 1) {
                        update_comment_meta($post_id, "_user_comment_liked", $post_users);
                    } else {
                        update_post_meta($post_id, "_user_liked", $post_users);
                    }
                }
            } else { // user is anonymous
                $user_ip = sl_get_ip();
                $post_users = post_ip_likes($user_ip, $post_id, $is_comment);
                // Update Post
                if ($post_users) {
                    $uip_key = array_search($user_ip, $post_users);
                    unset($post_users[$uip_key]);
                    if ($is_comment == 1) {
                        update_comment_meta($post_id, "_user_comment_IP", $post_users);
                    } else {
                        update_post_meta($post_id, "_user_IP", $post_users);
                    }
                }
            }
            $like_count = ($count > 0) ? --$count : 0; // Prevent negative number
            $response['status'] = "unliked";
            $response['icon'] = get_unliked_icon();
        }
        if ($is_comment == 1) {
            update_comment_meta($post_id, "_comment_like_count", $like_count);
            update_comment_meta($post_id, "_comment_like_modified", date('Y-m-d H:i:s'));
        } else {
            update_post_meta($post_id, "_post_like_count", $like_count);
            update_post_meta($post_id, "_post_like_modified", date('Y-m-d H:i:s'));
        }
        $response['count'] = get_like_count($like_count);
        $response['testing'] = $is_comment;
        if ($disabled == true) {
            if ($is_comment == 1) {
                wp_redirect(get_permalink(get_the_ID()));
                exit();
            } else {
                wp_redirect(get_permalink($post_id));
                exit();
            }
        } else {
            wp_send_json($response);
        }
    }
}

/**
 * Utility to test if the post is already liked
 * @since    0.5
 */
function already_liked($post_id, $is_comment)
{
    $post_users = NULL;
    $user_id = NULL;
    if (is_user_logged_in()) { // user is logged in
        $user_id = get_current_user_id();
        $post_meta_users = ($is_comment == 1) ? get_comment_meta($post_id, "_user_comment_liked") : get_post_meta($post_id, "_user_liked");
        if (count($post_meta_users) != 0) {
            $post_users = $post_meta_users[0];
        }
    } else { // user is anonymous
        $user_id = sl_get_ip();
        $post_meta_users = ($is_comment == 1) ? get_comment_meta($post_id, "_user_comment_IP") : get_post_meta($post_id, "_user_IP");
        if (count($post_meta_users) != 0) { // meta exists, set up values
            $post_users = $post_meta_users[0];
        }
    }
    if (is_array($post_users) && in_array($user_id, $post_users)) {
        return true;
    } else {
        return false;
    }
} // already_liked()

/**
 * Output the like button
 * @since    0.5
 */
function get_simple_likes_button($post_id, $is_comment = NULL)
{
    $is_comment = (NULL == $is_comment) ? 0 : 1;
    $output = '';
    $nonce = wp_create_nonce('simple-likes-nonce'); // Security
    if ($is_comment == 1) {
        $post_id_class = esc_attr(' sl-comment-button-' . $post_id);
        $comment_class = esc_attr(' sl-comment');
        $like_count = get_comment_meta($post_id, "_comment_like_count", true);
        $like_count = (isset($like_count) && is_numeric($like_count)) ? $like_count : 0;
    } else {
        $post_id_class = esc_attr(' sl-button-' . $post_id);
        $comment_class = esc_attr('');
        $like_count = get_post_meta($post_id, "_post_like_count", true);
        $like_count = (isset($like_count) && is_numeric($like_count)) ? $like_count : 0;
    }
    $count = get_like_count($like_count);
    $icon_empty = get_unliked_icon();
    $icon_full = get_liked_icon();
    // Loader
    $loader = '';
    // Liked/Unliked Variables
    if (already_liked($post_id, $is_comment)) {
        $class = esc_attr(' liked');
        $title = __('Unlike', 'YourThemeTextDomain');
        $icon = $icon_full;
    } else {
        $class = '';
        $title = __('Like', 'YourThemeTextDomain');
        $icon = $icon_empty;
    }
    $output = '' . $icon . $count . '' . $loader . '';
    return $output;
} // get_simple_likes_button()

/**
 * Processes shortcode to manually add the button to posts
 * @since    0.5
 */
add_shortcode('jmliker', 'sl_shortcode');
function sl_shortcode()
{
    return get_simple_likes_button(get_the_ID(), 0);
} // shortcode()

/**
 * Utility retrieves post meta user likes (user id array),
 * then adds new user id to retrieved array
 * @since    0.5
 */
function post_user_likes($user_id, $post_id, $is_comment)
{
    $post_users = '';
    $post_meta_users = ($is_comment == 1) ? get_comment_meta($post_id, "_user_comment_liked") : get_post_meta($post_id, "_user_liked");
    if (count($post_meta_users) != 0) {
        $post_users = $post_meta_users[0];
    }
    if (!is_array($post_users)) {
        $post_users = array();
    }
    if (!in_array($user_id, $post_users)) {
        $post_users['user-' . $user_id] = $user_id;
    }
    return $post_users;
} // post_user_likes()

/**
 * Utility retrieves post meta ip likes (ip array),
 * then adds new ip to retrieved array
 * @since    0.5
 */
function post_ip_likes($user_ip, $post_id, $is_comment)
{
    $post_users = '';
    $post_meta_users = ($is_comment == 1) ? get_comment_meta($post_id, "_user_comment_IP") : get_post_meta($post_id, "_user_IP");
    // Retrieve post information
    if (count($post_meta_users) != 0) {
        $post_users = $post_meta_users[0];
    }
    if (!is_array($post_users)) {
        $post_users = array();
    }
    if (!in_array($user_ip, $post_users)) {
        $post_users['ip-' . $user_ip] = $user_ip;
    }
    return $post_users;
} // post_ip_likes()

/**
 * Utility to retrieve IP address
 * @since    0.5
 */
function sl_get_ip()
{
    if (isset($_SERVER['HTTP_CLIENT_IP']) && !empty($_SERVER['HTTP_CLIENT_IP'])) {
        $ip = $_SERVER['HTTP_CLIENT_IP'];
    } elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && !empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
    } else {
        $ip = (isset($_SERVER['REMOTE_ADDR'])) ? $_SERVER['REMOTE_ADDR'] : '0.0.0.0';
    }
    $ip = filter_var($ip, FILTER_VALIDATE_IP);
    $ip = ($ip === false) ? '0.0.0.0' : $ip;
    return $ip;
} // sl_get_ip()

/**
 * Utility returns the button icon for "like" action
 * @since    0.5
 */
function get_liked_icon()
{
    /* If already using Font Awesome with your theme, replace svg with:  */
    $icon = '♥';
    return $icon;
} // get_liked_icon()

/**
 * Utility returns the button icon for "unlike" action
 * @since    0.5
 */
function get_unliked_icon()
{
    /* If already using Font Awesome with your theme, replace svg with:  */
    $icon = '♥';
    return $icon;
} // get_unliked_icon()

/**
 * Utility function to format the button count,
 * appending "K" if one thousand or greater,
 * "M" if one million or greater,
 * and "B" if one billion or greater (unlikely).
 * $precision = how many decimal points to display (1.25K)
 * @since    0.5
 */
function sl_format_count($number)
{
    $precision = 2;
    if ($number >= 1000 && $number < 1000000) {
        $formatted = number_format($number / 1000, $precision) . 'K';
    } else if ($number >= 1000000 && $number < 1000000000) {
        $formatted = number_format($number / 1000000, $precision) . 'M';
    } else if ($number >= 1000000000) {
        $formatted = number_format($number / 1000000000, $precision) . 'B';
    } else {
        $formatted = $number; // Number is less than 1000
    }
    $formatted = str_replace('.00', '', $formatted);
    return $formatted;
} // sl_format_count()

/**
 * Utility retrieves count plus count options,
 * returns appropriate format based on options
 * @since    0.5
 */
function get_like_count($like_count)
{
    $like_text = __('محبوب', 'YourThemeTextDomain');
    if (is_numeric($like_count) && $like_count > 0) {
        $number = sl_format_count($like_count);
    } else {
        $number = $like_text;
    }
    $count = '' . $number . '';
    return $count;
} // get_like_count()

// User Profile List
add_action('show_user_profile', 'show_user_likes');
add_action('edit_user_profile', 'show_user_likes');
function show_user_likes($user)
{ ?>
    
true)); $args = array( 'numberposts' => -1, 'post_type' => $types, 'meta_query' => array( array( 'key' => '_user_liked', 'value' => $user->ID, 'compare' => 'LIKE' ) )); $sep = ''; $like_query = new WP_Query($args); if ($like_query->have_posts()) : ?>

have_posts()) : $like_query->the_post(); echo $sep; ?>

یک فایل با نام simple-likes-public.js در روت اصلی پوسته درست کنید و سپس محتویات زیر را در آن قرار دهید :

(function( $ ) {
    'use strict';
    $(document).on('click', '.sl-button', function() {
        var button = $(this);
        var post_id = button.attr('data-post-id');
        var security = button.attr('data-nonce');
        var iscomment = button.attr('data-iscomment');
        var allbuttons;
        if ( iscomment === '1' ) { /* Comments can have same id */
            allbuttons = $('.sl-comment-button-'+post_id);
        } else {
            allbuttons = $('.sl-button-'+post_id);
        }
        var loader = allbuttons.next('#sl-loader');
        if (post_id !== '') {
            $.ajax({
                type: 'POST',
                url: simpleLikes.ajaxurl,
                data : {
                    action : 'process_simple_like',
                    post_id : post_id,
                    nonce : security,
                    is_comment : iscomment,
                },
                beforeSend:function(){
                    loader.html(' 
Loading...
'); }, success: function(response){ var icon = response.icon; var count = response.count; allbuttons.html(icon+count); if(response.status === 'unliked') { var like_text = simpleLikes.like; allbuttons.prop('title', like_text); allbuttons.removeClass('liked'); } else { var unlike_text = simpleLikes.unlike; allbuttons.prop('title', unlike_text); allbuttons.addClass('liked'); } loader.empty(); } }); } return false; }); })( jQuery );

سپس کد زیر را قبل از بسته شدن تگ head قرار دهید :

%MINIFYHTML9e54a7d7ae97cfd130ecaa56ebe05c8021%

برای استایل دادن به دکمه لایک نیز کد زیر را به فایل style.css قالب در انتهای بقیه کد ها قرار بدهید :

/* Wrapper */
.sl-wrapper a {
    border-bottom: 0 !important;
    text-decoration: none !important;
}
.sl-button {
	padding: 0.375em 0.625em;
	font-size: 18px;
	line-height: 1;
	font-weight: normal;
	
}
/* Colors */
a.liked {
    color: #da1b1b;
}
a.liked:hover,
a.liked:active,
a.liked:focus {
    color: #666666;
}
a.liked span.sl-count,
.sl-count {
    color: #666666;
}

/* Icon */
.sl-icon {
    margin-right: 0.3125em;
    font-family: 'Arial Unicode MS', Arial, sans-serif;
    vertical-align: middle;
}
.sl-icon svg {
    fill: currentColor;
    width: 1em;
    height: 1em;
}
.sl-icon svg:after {
    content: "";
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left:0;
}

/* Count */
.sl-count {
    font-size: 0.625em;
    font-family: Arial, 'Helvetica Neue', Helvetica, sans-serif;
    text-transform: uppercase;
    vertical-align: middle;
}

/* Loader */
.loader,
.loader:before,
.loader:after {
    background: rgba(0, 0, 0, 0.2);
    -webkit-animation: load1 1s infinite ease-in-out;
    animation: load1 1s infinite ease-in-out;
    width: .2em;
    height: .6em;
}
.loader:before,
.loader:after {
    position: absolute;
    top: 0;
    content: '';
}
.loader:before {
    left: -.375em;
    -webkit-animation-delay: -0.32s;
    animation-delay: -0.32s;
}
.loader {
    text-indent: -9999em;
    display: inline-block;
    position: relative;
    vertical-align: middle;
    font-size: 1em;
    -webkit-transform: translateZ(0);
    -ms-transform: translateZ(0);
    transform: translateZ(0);
    -webkit-animation-delay: -0.16s;
    animation-delay: -0.16s;
}
.loader:after {
    left: .375em;
}
@-webkit-keyframes load1 {
    0%,
    80%,
    100% {
        box-shadow: 0 0 rgba(0, 0, 0, 0.2);
        height: .6em;
    }
    40% {
        box-shadow: 0 -.3em rgba(0, 0, 0, 0.2);
        height: 1em;
    }
}
@keyframes load1 {
    0%,
    80%,
    100% {
        box-shadow: 0 0 rgba(0, 0, 0, 0.2);
        height: .6em;
    }
    40% {
        box-shadow: 0 -.3em rgba(0, 0, 0, 0.2);
        height: 1em;
    }
}

خب دیگه تقریبا کار تمومه :)‌ در فایل single.php در حلقه نوشته تکه کد زیر را فراخوانی کنید .

  
 

هنوز تموم نشده ! برای اضافه کردن دکمه لایک به نظرات هم تکه کد زیر استفاده کنید :

 echo get_simple_likes_button( get_comment_ID(), 1 );
 
اشتراک گذاری:
برچسب ها: اضافه کردن دکمه لایک به پست وردپرس

مطالب زیر را حتما بخوانید
  • آموزش ساخت پوسته وردپرس به صورت قدم به قدم (در حال تکمیل)

    51 بازدید

  • بهترین روش پیشنمایش قالب های وردپرس برای توسعه دهندگان

    67 بازدید

  • فیلم آموزش طراحی و ساخت سایت وردپرسی در هاست اشتراکی cpanel

    37 بازدید

  • اضافه کردن یک هشدار یا اطلاعیه در داشبورد وردپرس

    41 بازدید

  • امکانات وردپرس فارسی جدید 4.9.2

    35 بازدید

دیدگاه بگذارید

avatar
avatar
  اشتراک  
به من اطلاع بده
جستجو برای:
  • محبوب
  • جدید
  • دیدگاه ها
قبلی بعدی
جدیدترین نوشته ها
  • اولین کنفرانس PHP در جهان – آیا PHP 8 رونمایی می شود ؟ 1398/08/12
  • آموزش ٬ معرفی و دانلود فونت وزیر – وب و PC 1398/08/08
  • تست ارتباط ‍پایگاه داده mysql با لاراول 1398/06/17
  • گرفتن تمام مقادیر POST$ و یا GET$ در PHP و ذخیره کردن در یک فایل TEXT 1398/06/08
  • افزایش سرعت چهار تا ده برابری سایت وردپرسی شما (حذف علامت سوال از فایل) + ویدئو آموزشی 1398/05/24
سبد خرید
درباره آریاپاد

آریاپاد گروهی متشکل از برنامه نویسان Back-End است بر بستر زبان برنامه نویسی PHP و مدیریت سرور های لینوکسی – بخش عمده ای از کار ما فعالیت بر بستر حفظ امنیت سایت های پارسی است .

  • location_on
    کرمان شهرستان رفسنجان
  • phone_android
    ۰۹۱۳۶۵۲۳۵۹۵
  • email
    support@ariapad.com
دسترسی سریع
  • مجله آموزشی برنامه نویسی
  • بلاگ
  • دوره ها
  • حساب کاربری
  • سبد خرید
  • پرداخت
  • جداول قیمت گذاری
نماد اعتماد

تمامی حقوق برای گروه توسعه آٰریاپاد محفوظ می باشد.
keyboard_arrow_up wpDiscuz