用户关注功能可能最早出现在的是微博上,登录用户可以关注别人,自己也可以被关注,那们使用wordpress开发带用户注册的主题的时候是否也可以实现关注功能的,答案是肯定的,今天就给大家分享一下使用纯代码实现wordpress用户关注功能的实现方法。首先,将下面的代码放到您的functions.PHP文件中。
/**
* 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(),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(),implode(",",$guanzhu));
update_user_Meta($user,$bguanzhu));
exit(json_encode(['msg'=>'取消成功!']));
}else{
array_push($guanzhu,$user);
array_push($bguanzhu,get_current_user_id());
update_user_Meta(get_current_user_id(),$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,true);
if($Meta){
$guanzhu = explode(",get_user_Meta($authorID,true));
return count($guanzhu);
}else{
return 0;
}
}
function get_wpdiary_bguanzhu_count($authorID){
$Meta = get_user_Meta($authorID,true);
if($Meta){
$bguanzhu = explode(",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(),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 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,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:;" 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,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(),true);
$user = $fensi->ID;
if($Meta){
$guanzhu = explode(',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 } ?>好了,就这些了,用心的朋友仔细看一看代码还会让你的关注功能更加的丰富,希望对大家有用!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。