纯代码为wordpress添加点赞功能;测速网亲测可用,并且已经应用到wp主题开发中;
wordpress的文章点赞方法很多,而且大多数都是依赖插件和第三方的附属功能。说起来也是挺麻烦的,原本只是想添加一个点赞最后还附带过来无用的第三方广告功能;所以测速网分享出来一款非常简洁的纯代码实现wp主题点赞的功能。而且操作起来非常简单,有点基本的小伙伴都可以直接上手。而且运用cookies有效的解决了重复点赞bug;下面就是方法喽。
添加函数前注意要引用版本为1.10版以上的jQuery哦
首先需要在Functions.php文件中添加一个注册函:
area data-settings="dblclick" readonly >add_action('wp_ajax_nopriv_bigfa_like', 'bigfa_like');add_action('wp_ajax_bigfa_like', 'bigfa_like');function bigfa_like(){global $wpdb,$post;$id = $_POST["um_id"];$action = $_POST["um_action"];if ( $action == 'ding'){$bigfa_raters = get_post_meta($id,'bigfa_ding',true);$expire = time() + 99999999;$domain = ($_SERVER['HTTP_HOST'] != 'localhost') ? $_SERVER['HTTP_HOST'] : false; // make cookies work with localhostsetcookie('bigfa_ding_'.$id,$id,$expire,'/',$domain,false);if (!$bigfa_raters || !is_numeric($bigfa_raters)) {update_post_meta($id, 'bigfa_ding', 1);} else {update_post_meta($id, 'bigfa_ding', ($bigfa_raters + 1));} echo get_post_meta($id,'bigfa_ding',true);} die;}area>161718192021222324add_action('wp_ajax_nopriv_bigfa_like', 'bigfa_like');add_action('wp_ajax_bigfa_like', 'bigfa_like');function bigfa_like(){global $wpdb,$post;$id = $_POST["um_id"];$action = $_POST["um_action"];if ( $action == 'ding'){$bigfa_raters = get_post_me
ta($id,'bigfa_ding',true);$expire = time() + 99999999;$domain = ($_SERVER['HTTP_HOST'] != 'localhost') ? $_SERVER['HTTP_HOST'] : false; // make coo
kies work with localhostsetcoo
kie('bigfa_ding_'.$id,$id,$expire,'/',$domain,false);if (!$bigfa_raters || !is_numeric($bigfa_raters)) {update_post_me
ta($id, 'bigfa_ding', 1);} else {update_post_me
ta($id, 'bigfa_ding', ($bigfa_raters + 1));} echo get_post_me
ta($id,'bigfa_ding',true);} die;}
然后在页头或是页角位置添加一段JS代码:
area data-settings="dblclick" readonly >$(document).ready(function() { $.fn.postLike = function() { if ($(this).hasClass('done')) { return false; } else { $(this).addClass('done'); var id = $(this).data("id"), action = $(this).data('action'), rateHolder = $(this).children('.count'); var ajax_data = { action: "bigfa_like", um_id: id, um_action: action }; $.post("./wp-admin/admin-ajax.php", ajax_data, function(data) { $(rateHolder).html(data); }); return false; }};$(document).on("click", ".favorite",function() { $(this).postLike();});}); area>1617181920212223242526$(docu
ment).ready(function() { $.fn.postLike = function() { if ($(this).hasClass('done')) { return false; } else { $(this).addClass('done'); var id = $(this).data("id"), action = $(this).data('action'), rateHolder = $(this).children('.count'); var ajax_data = { action: "bigfa_like", um_id: id, um_action: action }; $.post("./wp-admin/admin-ajax.php", ajax_data, function(data) { $(rateHolder).html(data); }); return false; }};$(docu
ment).on("click", ".favorite",function() { $(this).postLike();});});
最后在文章页面添加下前台点赞的调用代码:
area data-settings="dblclick" readonly > <div > <a href="javascript:;" data-action="ding" data-id="<?php the_ID(); ?>" >测速网好帅 <span ><?php if( get_post_meta($post->ID,'bigfa_ding',true) ){echo get_post_meta($post->ID,'bigfa_ding',true); } else {echo '0'; }?></span></a> </div>area>123456789 <div > <a href="javas
cript:;" data-action="ding" data-id="<?php the_ID(); ?>" >测速网好帅 <span ><?php if( get_post_me
ta($post->ID,'bigfa_ding',true) ){echo get_post_me
ta($post->ID,'bigfa_ding',true); } else {echo '0'; }?></span></a> </div>
提供一小段的参考CSS样式方便大家操作
area data-settings="dblclick" readonly >.post-like{text-align:center;padding:10px}.post-like a{ background-color:#21759B;border-radius: 3px;color: #FFFFFF;font-size: 12px;padding: 5px 10px;text-decoration: none;outline:none}.post-like a.done, .post-like a:hover{background-color:#eee;color:#21759B;} .post-like a.done{cursor:not-allowed}area>1234.post-like{text-align:center;padding:10px}.post-like a{ background-color:#21759B;border-radius: 3px;color: #FFFFFF;font-size: 12px;padding: 5px 10px;text-decoration: none;outline:none}.post-like a.done, .post-like a:hover{background-color:#eee;color:#21759B;} .post-like a.done{cursor:not-allowed}