WordPress.org

Ready to get started?Download WordPress

Forums

User Role Editor
[resolved] Set role programmatically (3 posts)

  1. otti.steinhauer
    Member
    Posted 1 year ago #

    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/

  2. Vladimir Garagulya
    Member
    Plugin Author

    Posted 1 year ago #

    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.

  3. otti.steinhauer
    Member
    Posted 1 year ago #

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

    Regards

    Otti

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic