dongwei3120 2019-04-11 23:21
浏览 82
已采纳

如何在注册时添加多个角色?

I'm trying to add 2 roles to user at registration. Role 1- One of 2 default, already preset roles. Role 2- Individually generated for every user at their registration.

When assigning default role only- assigns no problem. When i'm adding dynamically generated roles, it assigns this role only, missing the default one.

    $user = wp_insert_user( $userdata );
    $getuser = new WP_User($user); 
    $getuser->set_role('profile_1');
    add_role( 
    'user-ID'.$user, 
    __( 'user-ID'.$user, 'wordpress-66604-809452.cloudwaysapps.com' ), 
    array(
        'read' => true,
        'edit_posts' => true,
    ));
    $getuser->set_role('user-ID'.$user);
  • 写回答

1条回答 默认 最新

  • duanli0453 2019-04-12 03:00
    关注

    From what i'm reading set_role removes the roles and assign the new one ONLY, it seems like what you described

    https://codex.wordpress.org/Class_Reference/WP_User#set_role.28.24role.29

    Set Role

    set_role($role) Set the role of the user.

    This will remove the previous roles of the user and assign the user the new one. You can set the role to an empty string and it will remove all of the roles from the user.

    Parameters role (string) - role name. Returns Nothing Since 2.0.0 Related actions

    Add Role

    add_role( $role ) Add role to user.

    Updates the user's meta data option with capabilities and roles.

    Parameters role (string) - role name.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧