用户关注功能可能最早出现在的是微博上,登录用户可以关注别人,自己也可以被关注,那们使用 WordPress 开发带用户注册的主题的时候是否也可以实现关注功能的,答案是肯定的,今天就给大家分享一下使用纯代码实现 WordPress 用户关注功能的实现方法。首先,将下面的代码放到您的 functions.php 文件中。
- function wpdiary_user_guanzhu(){
- if(!get_current_user_id()){
- exit(json_encode(['msg'=>'请登录!']));
- }
- if( $_POST['user']==get_current_user_id()){
- exit(json_encode(['msg'=>'你不能关注自己!']));
- }
- $user = $_POST['user'];
- $meta = get_user_meta(get_current_user_id(),'guanzhu',true);
- $bguanzhu1 = explode(',',get_user_meta($user,'bguanzhu',true));
- $bguanzhu = array_filter($bguanzhu1);
- $guanzhu1 = explode(',',get_user_meta(get_current_user_id(),'guanzhu',true));
- $guanzhu = array_filter($guanzhu1);
- if(in_array($user,$guanzhu)){
- foreach($guanzhu as $k=>$v){
- if($v==$user){
- unset($guanzhu[$k]);
- }
- }
- foreach($bguanzhu as $k=>$v){
- if($v==get_current_user_id()){
- unset($bguanzhu[$k]);
- }
- }
- update_user_meta(get_current_user_id(),'guanzhu',implode(",",$guanzhu));
- update_user_meta($user,'bguanzhu',implode(",",$bguanzhu));
- exit(json_encode(['msg'=>'取消成功!']));
- }else{
- array_push($guanzhu,$user);
- array_push($bguanzhu,get_current_user_id());
- update_user_meta(get_current_user_id(),'guanzhu',implode(",",$guanzhu));
- update_user_meta($user,'bguanzhu',implode(",",$bguanzhu));
- exit(json_encode(['msg'=>'关注成功!']));
- }
- }
- add_action('wp_ajax_guanzhu','wpdiary_user_guanzhu');
- add_action('wp_ajax_nopriv_guanzhu','wpdiary_user_guanzhu');
- //获取指定用户关注数量
- function get_wpdiary_guanzhu_count($authorID){
- $meta = get_user_meta($authorID,'guanzhu',true);
- if($meta){
- $guanzhu = explode(",",get_user_meta($authorID,'guanzhu',true));
- return count($guanzhu);
- }else{
- return 0;
- }
- }
- //获取指定用户被关注数量
- function get_wpdiary_bguanzhu_count($authorID){
- $meta = get_user_meta($authorID,'bguanzhu',true);
- if($meta){
- $bguanzhu = explode(",",get_user_meta($authorID,'bguanzhu',true));
- return count($bguanzhu);
- }else{
- return 0;
- }
- }
其次,消息提示使用了 layer 这个 jQuery 弹出层插件,所以,您还需要在主题中引入这个框架的文件(下载地址:http://res.layui.com/static/download/layer/layer-v3.1.1.zip),下载后把整个文件夹放到您的主题目录中,然后在主题头部引入 layer.js,并将下面的 JS 代码放到您的 footer.php 中。
- <script>
- $('a.addfollow').click(function(){
- var user = $(this).attr('user');
- $.ajax({
- url: "/wp-admin/admin-ajax.php",
- type:'post',
- dataType:'json',
- data:{action:'guanzhu',user:user},
- success: function(data){
- layer.alert(data.msg, function(){
- location.reload();
- });
- }
- });
- });
- </script>
第三,将关注按钮放到您需要放的地方,比如用户列表中或作者存档页,关注按钮代码如下:
- <?php
- $meta = get_user_meta(get_current_user_id(),'guanzhu',true);
- $user = ‘要被关注的用户的ID’
- if($meta){
- $guanzhu = explode(',',$meta);
- }else{
- $guanzhu = array();
- }
- if(in_array($user,$guanzhu)){?>
- <a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" user="<?php echo $user;?>" class="addfollow focus-link">+取消关注</a>
- <?php }else{?>
- <a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" user="<?php echo $user;?>" class="addfollow focus-link">+ 关注</a>
- <?php }?>
第四,获取指定用户关注和被关注数量:
- <?php echo get_wpdiary_guanzhu_count(‘用户ID’);?>//获取指定用户关注数量
- <?php echo get_wpdiary_bguanzhu_count(‘用户ID’);?>//获取指定用户被关注数量
第五,如何获取当前登录用户关注的用户的列表:
- <?php
- $user_id = get_current_user_id();//当前登录用户id,也可以指定为某个ID的用户,这样获取到的就是指定ID的用户的关注列表
- $meta = get_user_meta($user_id,'guanzhu',true);
- $guanzhulist = explode(',',$meta );
- $users = get_users(array('include'=>$guanzhulist));
- ?>
- <?php foreach($users as $user){ ?>
- <li class="pic-btn-li">
- <div class="li-pic">
- <a href="<?php%20echo%20get_author_posts_url(%20$user->ID);%20?>/?userid=<?php%20echo%20$user->ID;?>" target="_blank" class="li-link">
- <?php echo get_avatar($user->ID);?>
- </a>
- </div>
- <div class="li-dec">
- <p class="dec-name">
- <a href="<?php%20echo%20get_author_posts_url(%20$user->ID);%20?>/?userid=<?php%20echo%20$user->ID;?>" target="_blank" class="dec-name-link"><?php echo $user->nickname;?></a>
- </p>
- <p class="dec-sub">
- <span class="dec-fans">粉丝 <?php echo get_wpdiary_bguanzhu_count($user->ID);?></span>
- </p>
- </div>
- <a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" user="<?php echo $user->ID;?>" class="addfollow sp-mod-button" title="关注他">取消</a>
- </li>
- <?php } ?>
第六,获取关注过当前登录用户的列表(被关注列表,也可以说是粉丝列表):
- <?php
- $user_id = get_current_user_id();//当前登录用户id,也可以指定为某个ID的用户,这样获取到的就是关注过指定ID的用户列表(粉丝列表)
- $bguanzhu = get_user_meta($user_id,'bguanzhu',true);
- $fanslist = explode(',',$bguanzhu );
- $fans = get_users(array('include'=>$fanslist));
- ?>
- <?php foreach($fans as $fensi){ ?>
- <li class="pic-btn-li">
- <div class="li-pic">
- <a href="<?php%20echo%20get_author_posts_url(%20$fensi->ID);%20?>/?userid=<?php%20echo%20$fensi->ID;?>" target="_blank" class="li-link">
- <?php echo get_avatar($fensi->ID);?>
- </a>
- </div>
- <div class="li-dec">
- <p class="dec-name">
- <a href="<?php%20echo%20get_author_posts_url(%20$fensi->ID);%20?>/?userid=<?php%20echo%20$fensi->ID;?>" target="_blank" class="dec-name-link"><?php echo $fensi->nickname;?></a>
- </p>
- <p class="dec-sub">
- <span class="dec-fans">粉丝 <?php echo get_wpdiary_bguanzhu_count($fensi->ID);?></span>
- </p>
- </div>
- <?php
- $meta = get_user_meta(get_current_user_id(),'guanzhu',true);
- $user = $fensi->ID;
- if($meta){
- $guanzhu = explode(',',get_user_meta(get_current_user_id(),'guanzhu',true));
- }else{
- $guanzhu = array();
- }
- if(in_array($user,$guanzhu)){?>
- <a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" user="<?php echo $fensi->ID;?>" class="addfollow sp-mod-button" title="取消关注">已互关</a>
- <?php }else{?>
- <a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" user="<?php echo $fensi->ID;?>" class="addfollow sp-mod-button" title="关注他">关注他</a>
- <?php }?>
- </li>
- <?php } ?>
好了,就这些了,用心的朋友仔细看一看代码还会让你的关注功能更加的丰富,希望对大家有用!
WordPress 纯代码实现用户关注功能
- 此资源下载价格为2元立即购买(VIP 免费)立即升级