Support » Plugin: User Role Editor » Set role programmatically

  • Resolved otti.steinhauer

    (@ottisteinhauer)


    Hello,
    there are two kinds of user-roles I want to set in a plugin. First there is the role ‘kunde’, that’s no problem. It is the default role for a new user.
    The second one doesn’t work. It is the role ‘vermittler’ and I don’t know how to add this role.
    My Code for insert:` $user_data = array( ‘ID’ => ”,
    ‘user_pass’ => $_POST[‘vermittlerpasswort’],
    ‘user_login’ => $_POST[‘vermittlername’],
    ‘user_nicename’ => $_POST[‘vermittlername’],
    ‘user_email’ => $_POST[’email’],
    ‘last_name’ => $nachname,
    ‘first_name’ => $vorname,
    ‘user_registered’ => $datum,
    ‘role’ => get_option(‘default_role’)
    );
    $user_id = wp_insert_user( $user_data );`
    The code inserts a user with ‘Kunde’-role. For ‘Vermittler’-role I tested:

    'role' => get_option('Vermittler_role')
    			       	 	 'role' => get_option('Vermittler')

    nothing works. Si I tried first to set ‘Kunde’-role and then change. // die Rolle auf Vermittler ändern
    $u = new WP_User( $user_id );
    // Remove role
    $u->remove_role( ‘Kunde’ );

    // Add role
    $u->add_role( ‘Vermittler’ );`
    doesn’t work, too.
    What’s wrong?
    Please excuse my very bad English, I hope You can undestand what I mean.

    Otti

    http://wordpress.org/extend/plugins/user-role-editor/

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Vladimir Garagulya

    (@shinephp)

    Hi,

    Does ‘Vermittler’ role exists?
    1st, try to use lowercase role id in remove_role, add_role. WP role has 2 separate attributes name and id. Probably id is ‘vermittler’, not ‘Vermitler’.

    Regards,
    Vladimir.

    Thread Starter otti.steinhauer

    (@ottisteinhauer)

    Hi,
    thank you very much for your kind reply. That was the solution – use ‘vermittler’ instead of ‘Vermittler’.

    Regards

    Otti

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Set role programmatically’ is closed to new replies.