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 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?