当前位置:首页 »“秋了秋”个人博客 » WP教程 » 纯代码实现Wordpress顶踩(投票)功能

纯代码实现Wordpress顶踩(投票)功能

作者:秋了秋 发表时间:2015年03月28日

在主题文件夹下的function.php添加如下代码:

  • function ashu_load_theme() {global $pagenow;if ( is_admin() && 'themes.php' == $pagenow && isset( $_GET['activated'] ) ) ashu_vote_install(); } add_action( 'load-themes.php', 'ashu_load_theme' );function ashu_vote_install(){global $wpdb;  $table_name = $wpdb->prefix . 'post_vote';if( $wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name ) : $sql = " CREATE TABLE `".$wpdb->prefix."post_vote` ( `id` BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY ,`user` INT NOT NULL ,`post` INT NOT NULL ,`rating` varchar(10),`ip` varchar(40) ) ENGINE = MYISAM DEFAULT CHARSET=utf8;"; require_once(ABSPATH . 'wp-admin/includes/upgrade.php');dbDelta($sql);endif;}
  • function add_vote($post_id,$user_id='',$ip='',$rating='up'){global $wpdb;$user_id = (int)$user_id; $post_id = (int)$post_id;if(($user_id=='')&&($ip=='')){return "e";} if($user_id!=''){$check= "select * from ".$wpdb->prefix."post_vote where post='$post_id' and user='$user_id'";}else{ if($ip!=''){$check= "select * from ".$wpdb->prefix."post_vote where post='$post_id' and ip='$ip'";}} $coo = $wpdb->get_results($check); if($rating=='up'){$rating='up';}else{$rating='down';} if(!count($coo) > 0){$s = "insert into ".$wpdb->prefix."post_vote (user,post,rating,ip) values('$user_id','$post_id','$rating','$ip')"; $wpdb->query($s);return "y";}else{return "h";}return "e";} function get_post_vote($post_id,$vote='up'){global $wpdb;$post_id = (int)$post_id; if($vote == 'up'){$vote='up';}else{$vote='down';} $sql = "select count(*) from ".$wpdb->prefix."post_vote where post='$post_id' and rating='$vote'"; $coo = $wpdb->get_var($sql);if($coo) return $coo;else return 0;}add_action("wp_ajax_vote_post", "add_votes_options"); add_action("wp_ajax_nopriv_vote_post", "add_votes_options"); function add_votes_options() { if( isset($_POST['action']) && ($_POST['action'] == 'vote_post') ){$postid = (int)$_POST['postid'];if( !$postid ){echo 'e'; die(0);}  $voted = $_COOKIE["smzdm_voted_".$postid];if( $voted ){echo 'h';die(0);}$ip = $_SERVER['REMOTE_ADDR'];$rating = $_POST['rating'];  if( is_user_logged_in() ){global $wpdb, $current_user;get_currentuserinfo(); $uid = $current_user->ID;}else{$uid='';}if($rating=='up'){$rating='up';}else{$rating='down';}  $voted = add_vote($postid,$uid,$ip,$rating);if($voted=='y'){setcookie("ashu_voted_" . $postid,$rating, time() + 3000000, '/'); echo 'y';die(0);}if($voted=='h'){setcookie("ashu_voted_" . $postid,$rating, time() + 3000000, '/');echo 'h';die(0);} if($voted=='e'){echo 'n';die(0);}}else{echo 'e';}die(0);}

将下面的代码放到我们需要显示wordpress顶和踩功能的地方,大家适量的修改即可!

  • <span class="vote_up" id="<?php echo 'vote_up'.$post->ID;?>">
  • <a href="javascript:void(0);" rel="<?php echo 'up_',$post->ID;?>">
  • <span id="<?php echo 'vup'.$post->ID;?>"><?php echo get_post_vote($post->ID,'up');?></span></a></span>
  •  
  • <span class="vote_down" id="<?php echo 'vote_down'.$post->ID;?>">
  • <a href="javascript:void(0);" rel="<?php echo 'down_'.$post->ID;?>">
  • <span id="<?php echo 'vdown'.$post->ID;?>"><?php echo get_post_vote($post->ID,'down');?></span></a></span>
  • <script type="text/javascript">var ajax_url = '<?php echo admin_url(); ?>admin-ajax.php';</script>
  • <script src="<?php echo get_template_directory_uri();?>/js/ding.js"></script>

然后新建一个js文件把以下内容复制进去:

    1. function getCookie(name) { var start = document.cookie.indexOf( name + "=" );var len = start + name.length + 1; if ( ( !start ) && (name!=document.cookie.substring(0, name.length ))) return null; if ( start == -1 )return null; var end = document.cookie.indexOf( ';', len ); if ( end == -1 )end = document.cookie.length;return unescape( document.cookie.substring( len, end ) ); } function ashu_isCookieEnable() { var today = new Date();today.setTime( today.getTime() ); var expires_date = new Date( today.getTime() + (1000 * 60) ); document.cookie = 'ashu_cookie_test=test;expires=' + expires_date.toGMTString() + ';path=/'; var cookieEnable = (getCookie('ashu_cookie_test') == 'test') ? true : false; return cookieEnable; } jQuery(document).ready(function($) { var ashu_token = 1; $('.vote_up a').click(function(){ if( !ashu_isCookieEnable() ) { alert("很抱歉,您不能给本文投票!"); return; } if( ashu_token != 1 ) { alert("您的鼠标点得也太快了吧?!"); return false; } ashu_token = 0; var full_info = $(this).attr( 'rel' ); var arr_param = full_info.split( '_' ); $.ajax({ url:ajax_url, type:'POST', data:'action=vote_post&rating=' + arr_param[ 0 ] + '&postid=' + arr_param[ 1 ], success:function(results){ if(results=='n'){ alert('评价失败'); ashu_token = 1; } if (results=='y'){ var upd_vd = 'vup' + arr_param[ 1 ]; $('#'+upd_vd).text(parseInt($("#"+upd_vd).text())+1); ashu_token = 1; } if (results=='h'){ ashu_token = 1; alert('已经发表过评价了'); } if (results=='e'){ashu_token = 1;alert('评价失败');}}});}); $('.vote_down a').click(function(){if( !ashu_isCookieEnable() ) {alert("很抱歉,您不能给本文投票!");return;} if(ashu_token != 1) {alert("您的鼠标点得也太快了吧?!");return false;} ashu_token = 0; var full_info = $(this).attr( 'rel' ); var arr_param = full_info.split( '_' ); $.ajax({ url:ajax_url, type:'POST', data:'action=vote_post&rating=' + arr_param[ 0 ] + '&postid=' + arr_param[ 1 ], success:function(results){if(results=='n'){alert('评价失败');ashu_token = 1;} if (results=='y'){var upd_vd = 'vdown' + arr_param[ 1 ];$("#"+upd_vd).text(parseInt($("#"+upd_vd).text())+1);ashu_token = 1;} if (results=='h'){ashu_token = 1;alert('已经发表过评价了');} if (results=='e'){ashu_token = 1;alert('发生未知错误');}}});});});

然后保存命名为ding.js上传到主题目录下的js文件夹即可。

代码比较多,我就没用了,所以没有演示~不建议使用,建议参考~!

31
文章作者: “秋了秋”个人博客,本站鼓励原创。
转载请注明本文地址:http://netblog.cn/blog/311.html
目录: WP教程标签: 顶踩投票 17095次阅读

请求播放音乐,请点击播放

登 录
点击获取验证码
还没账号?点击这里