MErhaba arkadaşlar
Yaynladığım yazıya eklentisiz beğen butonu eklemek istiyorum. Araştırdım 2 tane buldum birtanesi çalıştı github'da buldum ama çok küçük düzenleyemedim de varmı nasıl yapılacağını bilen?
functions.php dosyasına eklenecek kodlar:
add_action('wp_ajax_nopriv_ayas-begen', 'begen');
add_action('wp_ajax_ayas-begen', 'begen');
wp_enqueue_script('makale_begen', get_template_directory_uri().'/js/begen.js', array('jquery'), '1.0', true );
wp_localize_script('makale_begen', 'ajax_var', array(
'url' => admin_url('admin-ajax.php'),
'nonce' => wp_create_nonce('ajax-nonce')
));
$begen_zaman_sayaci = 1440; // 1 gün
function begen(){
$nonce = $_POST['nonce'];
if ( ! wp_verify_nonce( $nonce, 'ajax-nonce' ) )
die ( 'Busted!');
if(isset($_POST['begen']))
{
$ip = $_SERVER['REMOTE_ADDR'];
$post_id = $_POST['post_id'];
$meta_IP = get_post_meta($post_id, "voted_IP");
$voted_IP = $meta_IP[0];
if(!is_array($voted_IP))
$voted_IP = array();
$meta_count = get_post_meta($post_id, "votes_count", true);
if(!begen_kontrol($post_id))
{
$voted_IP[$ip] = time();
update_post_meta($post_id, "voted_IP", $voted_IP);
update_post_meta($post_id, "votes_count", ++$meta_count);
echo $meta_count;
}
else
echo "already";
}
exit;
}
function begen_kontrol($post_id)
{
global $begen_zaman_sayaci;
$meta_IP = get_post_meta($post_id, "voted_IP");
$voted_IP = $meta_IP[0];
if(!is_array($voted_IP))
$voted_IP = array();
$ip = $_SERVER['REMOTE_ADDR'];
if(in_array($ip, array_keys($voted_IP)))
{
$time = $voted_IP[$ip];
$now = time();
if(round(($now - $time) / 60) > $begen_zaman_sayaci)
return false;
return true;
}
return false;
}
function ayasBegen($post_id){
$begenme_sayisi = get_post_meta($post_id, "votes_count", true);
$cikis = '<div class="makaleBegen"><a href="#" data-post_id="'.$post_id.'"><i class="fa fa-heart"></i>';
if(begen_kontrol($post_id))
$cikis .= '<p>Beğendin</p>';
else
$cikis .= '<p>Beğen !</p>';
if($begenme_sayisi != 0 || $begenme_sayisi != null)
$cikis .= '<span class="begeniSayisi">('.$begenme_sayisi.')</span>';
else
$cikis .= '<span class="begeniSayisi">'.$begenme_sayisi.'</span>';
$cikis .= '</a></div>';
return $cikis;
}temanızın js klasörüne begen.js adında dosya açıp şunları yazın:
jQuery(document).ready(function(){jQuery(".makaleBegen a").click(function(){return kalp=jQuery(this),post_id=kalp.data("post_id"),jQuery.ajax({type:"post",url:ajax_var.url,data:"action=ayas-begen&nonce="+ajax_var.nonce+"&begen=&post_id="+post_id,success:function(a){"already"!=a&&(kalp.addClass("voted"),kalp.find("p").html("Beğendin !"),kalp.find(".begeniSayisi").html("("+a+")"))}}),!1})});Daha sonra kullanmak istediğin sayfaya şunu ekleyin (single.php):
<?php echo ayasBegen(get_the_ID());?>