WordPress纯代码实现用户关注功能

 WordPress

为插件添加用户关注功能。

将代码添加到指定文件,如果是主题中添加关注功能,可添加到function.php文件中。

/**
 * WordPress 关注与被关注功能
 * https://www.wp-diary.com/761.html
 */
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;
    }
    
}

和文章收藏功能一样需要使用JS

<script type="text/javascript">
	(function($){
		$("a.addfollow").on('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();
		 });
		 }
 });
});
	})(jQuery);
</script>

如果之前已经引入了layer文件,则可以跳过。

layer jQuery 弹出层插件,最后还需要引入这个框架的文件,下载后把layer这个文件夹放到目录中,然后引用 layer.js文件。

下载地址:layer 发行版 - Gitee.com

关注与取消关注按钮

?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:;" user="<?php echo $user;?>" class="addfollow focus-link">+取消关注</a>
<?php }else{?>
    <a href="javascript:;" 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 echo get_author_posts_url( $user->ID); ?>/?userid=<?php echo $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 echo get_author_posts_url( $user->ID); ?>/?userid=<?php echo $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:;" 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 echo get_author_posts_url( $fensi->ID); ?>/?userid=<?php echo $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 echo get_author_posts_url( $fensi->ID); ?>/?userid=<?php echo $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:;" user="<?php echo $fensi->ID;?>" class="addfollow sp-mod-button" title="取消关注">已互关</a>
        <?php }else{?>
            <a href="javascript:;" user="<?php echo $fensi->ID;?>" class="addfollow sp-mod-button" title="关注他">关注他</a>
        <?php }?>
    </li>
<?php } ?>

正在研究如何对粉丝/关注列表分页。如果有了解的用户也可提供方法。

如未明确标明可商用请查证后使用!如需商业授权请自主联系版权方!

成为第一个回复的人

显示验证码
没有账号? 注册  忘记密码?
19
20
21
22
23
24
25
modal close image