概述
用户关注
功能可能最早出现在的是微博上,登录用户可以关注别人,自己也可以被关注,那们使用WordPress开发带用户注册的主题的时候是否也可以实现关注功能的,答案... @H_419_0@用户关注功能可能最早出现在的是微博上,登录用户可以关注别人,自己也可以被关注,那们使用wordpress开发带用户注册的主题的时候是否也可以实现关注功能的,答案是肯定的,今天就给大家分享一下使用纯
代码实现wordpress用户关注功能的实现方法。首先,将下面的代码放到您的functions.PHP文件中。@H_419_0@/**@H_419_0@* wordpress 关注与被关注功能@H_419_0@* https://www.wp-diary.com/761.HTML@H_419_0@*/@H_419_0@function wpdiary_user_guanzhu(){@H_419_0@if(!get_current_user_ID()){@H_419_0@exit(Json_encode(['msg'=>'请登录!']));@H_419_0@}@H_419_0@if( $_POST['user']==get_current_user_ID()){@H_419_0@exit(Json_encode(['msg'=>'你不能关注自己!']));@H_419_0@}@H_419_0@$user = $_POST['user'];@H_419_0@$Meta = get_user_Meta(get_current_user_ID(),'guanzhu',true);@H_419_0@$bguanzhu1 = explode(',',get_user_Meta($user,'bguanzhu',true));@H_419_0@$bguanzhu = array_filter($bguanzhu1);@H_419_0@$guanzhu1 = explode(',get_user_Meta(get_current_user_ID(),true));@H_419_0@$guanzhu = array_filter($guanzhu1);@H_419_0@if(in_array($user,$guanzhu)){@H_419_0@foreach($guanzhu as $k=>$v){@H_419_0@if($v==$user){@H_419_0@unset($guanzhu[$k]);@H_419_0@}@H_419_0@}@H_419_0@foreach($bguanzhu as $k=>$v){@H_419_0@if($v==get_current_user_ID()){@H_419_0@unset($bguanzhu[$k]);@H_419_0@}@H_419_0@}@H_419_0@update_user_Meta(get_current_user_ID(),implode(",",$guanzhu));@H_419_0@update_user_Meta($user,$bguanzhu));@H_419_0@exit(Json_encode(['msg'=>'取消成功!']));@H_419_0@}else{@H_419_0@array_push($guanzhu,$user);@H_419_0@array_push($bguanzhu,get_current_user_ID());@H_419_0@update_user_Meta(get_current_user_ID(),$bguanzhu));@H_419_0@exit(Json_encode(['msg'=>'关注成功!']));@H_419_0@}@H_419_0@}@H_419_0@add_action('wp_AJAX_guanzhu','wpdiary_user_guanzhu');@H_419_0@add_action('wp_AJAX_nopriv_guanzhu','wpdiary_user_guanzhu');@H_419_0@//获取指定用户关注数量@H_419_0@function get_wpdiary_guanzhu_count($authorID){@H_419_0@$Meta = get_user_Meta($authorID,true);@H_419_0@if($Meta){@H_419_0@$guanzhu = explode(",get_user_Meta($authorID,true));@H_419_0@return count($guanzhu);@H_419_0@}else{@H_419_0@return 0;@H_419_0@}@H_419_0@}@H_419_0@//获取指定用户被关注数量@H_419_0@function get_wpdiary_bguanzhu_count($authorID){@H_419_0@$Meta = get_user_Meta($authorID,true);@H_419_0@if($Meta){@H_419_0@$bguanzhu = explode(",true));@H_419_0@return count($bguanzhu);@H_419_0@}else{@H_419_0@return 0;@H_419_0@}@H_419_0@}其次,消息提示使用了layer这个jqueryd出层插件,所以,您还需要在主题中引入这个框架的文件(下载地址:http://res.layui.com/static/download/layer/layer-v3.1.1.zip),下载后把整个文件夹放到您的主题目录中,然后在主题头部引入layer.Js,并将下面的Js代码放到您的footer.PHP中。@H_419_0@<script>@H_419_0@$('a.addfollow').click(function(){@H_419_0@var user = $(this).attr('user');@H_419_0@$.AJAX({@H_419_0@url: "/wp-admin/admin-AJAX.PHP",@H_419_0@type:'post',@H_419_0@dataType:'Json',@H_419_0@data:{action:'guanzhu',user:user},@H_419_0@success: function(data){@
[email protected](data.msg,function(){@
[email protected]();@H_419_0@});@H_419_0@}@H_419_0@});@H_419_0@});@H_419_0@</script>第三,将关注按钮放到您需要放的地方,比如用户列表中或作者存档页,关注按钮代码如下:@H_419_0@<?PHP@H_419_0@$Meta = get_user_Meta(get_current_user_ID(),true);@H_419_0@$user = ‘要被关注的用户的ID’@H_419_0@if($Meta){@H_419_0@$guanzhu = explode(',$Meta);@H_419_0@}else{@H_419_0@$guanzhu = array();@H_419_0@}@H_419_0@if(in_array($user,$guanzhu)){?>@H_419_0@<a href="JavaScript:;" user="<?PHP echo $user;?>" >+取消关注</a>@H_419_0@<?PHP }else{?>@H_419_0@<a href="JavaScript:;" user="<?PHP echo $user;?>" >+ 关注</a>@H_419_0@<?PHP }?>第四,获取指定用户关注和被关注数量@H_419_0@<?PHP echo get_wpdiary_guanzhu_count(‘用户ID’);?>//获取指定用户关注数量@H_419_0@<?PHP echo get_wpdiary_bguanzhu_count(‘用户ID’);?>//获取指定用户被关注数量第五,如何获取当前登录用户关注的用户的列表@H_419_0@<?PHP@H_419_0@$user_ID = get_current_user_ID();//当前登录用户ID,也可以指定为某个ID的用户,这样获取到的就是指定ID的用户的关注列表@H_419_0@$Meta = get_user_Meta($user_ID,true);@H_419_0@$guanzhuList = explode(',$Meta );@H_419_0@$users = get_users(array('include'=>$guanzhuList));@H_419_0@?>@H_419_0@<?PHP foreach($users as $user){ ?>@H_419_0@<li >@H_419_0@<div >@H_419_0@<a href="<?PHP%20echo%20get_author_posts_url(%20$user->ID);%20?>/?userID=<?PHP%20echo%20$user->ID;?>" target="_blank" >@H_419_0@<?PHP echo get_avatar($user->ID);?>@H_419_0@</a>@H_419_0@</div>@H_419_0@<div >@H_419_0@<p >@H_419_0@<a href="<?PHP%20echo%20get_author_posts_url(%20$user->ID);%20?>/?userID=<?PHP%20echo%20$user->ID;?>" target="_blank" ><?PHP echo $user->nickname;?></a>@H_419_0@</p>@H_419_0@<p >@H_419_0@<span >粉丝 <?PHP echo get_wpdiary_bguanzhu_count($user->ID);?></span>@H_419_0@</p>@H_419_0@</div>@H_419_0@<a href="JavaScript:;" user="<?PHP echo $user->ID;?>" title="关注他">取消</a>@H_419_0@</li>@H_419_0@<?PHP } ?>第六,获取关注过当前登录用户的列表(被关注列表,也可以说是粉丝列表)@H_419_0@<?PHP@H_419_0@$user_ID = get_current_user_ID();//当前登录用户ID,也可以指定为某个ID的用户,这样获取到的就是关注过指定ID的用户列表(粉丝列表)@H_419_0@$bguanzhu = get_user_Meta($user_ID,true);@H_419_0@$fansList = explode(',$bguanzhu );@H_419_0@$fans = get_users(array('include'=>$fansList));@H_419_0@?>@H_419_0@<?PHP foreach($fans as $fensi){ ?>@H_419_0@<li >@H_419_0@<div >@H_419_0@<a href="<?PHP%20echo%20get_author_posts_url(%20$fensi->ID);%20?>/?userID=<?PHP%20echo%20$fensi->ID;?>" target="_blank" >@H_419_0@<?PHP echo get_avatar($fensi->ID);?>@H_419_0@</a>@H_419_0@</div>@H_419_0@<div >@H_419_0@<p >@H_419_0@<a href="<?PHP%20echo%20get_author_posts_url(%20$fensi->ID);%20?>/?userID=<?PHP%20echo%20$fensi->ID;?>" target="_blank" ><?PHP echo $fensi->nickname;?></a>@H_419_0@</p>@H_419_0@<p >@H_419_0@<span >粉丝 <?PHP echo get_wpdiary_bguanzhu_count($fensi->ID);?></span>@H_419_0@</p>@H_419_0@</div>@H_419_0@<?PHP@H_419_0@$Meta = get_user_Meta(get_current_user_ID(),true);@H_419_0@$user = $fensi->ID;@H_419_0@if($Meta){@H_419_0@$guanzhu = explode(',true));@H_419_0@}else{@H_419_0@$guanzhu = array();@H_419_0@}@H_419_0@if(in_array($user,$guanzhu)){?>@H_419_0@<a href="JavaScript:;" user="<?PHP echo $fensi->ID;?>" title="取消关注">已互关</a>@H_419_0@<?PHP }else{?>@H_419_0@<a href="JavaScript:;" user="<?PHP echo $fensi->ID;?>" title="关注他">关注他</a>@H_419_0@<?PHP }?>@H_419_0@</li>@H_419_0@<?PHP } ?>好了,就这些了,用心的朋友仔细看一看代码还会让你的关注功能更加的丰富,希望对大家有用! 总结
以上是内存溢出为你收集整理的WordPress纯代码实现用户关注功能全部内容,希望文章能够帮你解决WordPress纯代码实现用户关注功能所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
评论列表(0条)