自学内容网 自学内容网

给Wordpress评论列表的用户昵称增加个性化角色称号和注册年数

什么是个性化角色称号?

个性化称号:其实就是对应wordpress的几个用户组,重新给它装个面具。

比如:管理员 -> 华山掌门

比如:订阅者 -> 华山弟子

比如:VIP组 -> 掌门亲传弟子

。。。

就是个好玩的东西

什么又是注册年数?

显示用户在你的网站上注册了多少年,就像CSDN的这个:

效果对比

默认的评论列表:

前端显示效果:

没有能力修改自定义后台的同学:

如果,你没有自定义后台,又实在想要这个功能,那么你可能需要手动修改代码完成每次用户头衔的变更。其实也就是一次的事情,我想你也不会总去改他吧。请把下面代码添加到 function.php 文件:

// 在评论列表 用户名后面 添加用户角色/注册年数/
// code by yangjiyong VX:uu0216

    if ( ! class_exists( 'Comment_Author_Role_Label' ) ) :
    class Comment_Author_Role_Label {
    public function __construct() {
    add_filter( 'get_comment_author', array( $this, 'get_comment_author_role' ), 10, 3 );
    add_filter( 'get_comment_author_link', array( $this, 'comment_author_role' ) );
    }
    // 获取评论角色
    function get_comment_author_role( $author, $comment_id, $comment ) { 
    $authoremail = get_comment_author_email( $comment); 
    $options = get_option('classic_options');
    $user_honor_admin = '超级大boss';//  管理员荣誉
    $user_honor_vip ='幕后大金主'; //  VIP组荣誉
    $user_honor_member = '入室小土豪'; //  会员组荣誉
    $user_honor_visiter = '踢馆的小子';         //  访客荣誉
    $user_join_age = '学徒';//  注册资历
    $user_name_age = '载';//  资历称谓
    $user_visiter ='闲人';//  未注册访客资历称谓
    // 检查用户是否注册
    if (email_exists($authoremail)) {
    //获取评论角色
    $user_roles_name = get_user_by( 'email', $authoremail );
    //获取评论角色id
    $the_user = get_user_by('email', $authoremail);
    $the_user_id = $the_user->ID;    
    //获取注册时间
    $user = get_userdata( $the_user_id );    
    $registerdate = get_date_from_gmt($user->user_registered);
    $time=time();
    // 给注册时间取整,返回 n 年
    $amount=date("Y",$time)-date("Y",strtotime($registerdate));
    if($amount <=0){
        $amount =0;
    }
    // 为了给css编号,超过10年的统一用10,先用10吧,我不知道自己的网站能不能用10年。
    $css_name = $amount;
    if($amount > 9){
        $css_name = 10;
    }

// 把0-9 转换成汉字
$arr = array('零', '一', '二', '三', '四', '五', '六', '七', '八', '九');
$result = null;
$integerArrLength = strlen($amount);
$result .= $arr[$amount];
$amount_txt = $user_join_age.$result.$user_name_age;
    $comment_user_role = $user_roles_name->roles[0];
    // 自定义角色身份显示名
    switch ($comment_user_role) {
        case 'administrator':
    $comment_user_role_text= $user_honor_admin;
    break;
    case 'vip': // vip组用户身份
    $comment_user_role_text= $user_hono

原文地址:https://blog.csdn.net/uuplay0216/article/details/140529926

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!