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

    (@shinephp)

    User roles and capabilities API is available from wp-includes/capabilities.php
    This function could be useful also:
    http://codex.wordpress.org/Function_Reference/get_editable_roles

    Thread Starter aarkhipov

    (@aarkhipov)

    Ok, I got it. There is the “role” argument in the wp_update_user function. We can use it to set a role programmatically:

    wp_update_user(array(
        'ID' => $user_id,
        'role' => 'my-custom-role'
    ));

    http://codex.wordpress.org/Function_Reference/wp_update_user

    Any word on what this would look like?

    I created a custom role with User Role Editor (called volunteer_user)

    And my code looks like this:

    add_action( 'edit_user_profile_update', 'my_save_extra_profile_fields' );
    
    function my_save_extra_profile_fields( $user_id ) {
    
    	if ( !current_user_can( 'edit_user', $user_id ) )
    		return false;
    	$user_id = wp_update_user(array('ID' => $user_id,'role' => 'volunteer_user'));
    }

    And when I click the button “Update User” on the user-edit.php page I don’t see that Role added to the usermeta in the capabilities.

    Am I doing something wrong?

    Any help would be appreciated!

    Plugin Author Vladimir Garagulya

    (@shinephp)

    Look at wp-includes/capabilities.php
    ‘wp_update_user’ function is too overloaded by other user attributes validation/update stuff. I did not test myself, so I’m not sure it it works just for these 2, user ID and role attributes.
    I think that solution below is more effective in this case:

    $user = new WP_User( $user_id );
    $user->set_role('volunteer_user');

    Thank you so much Vladimir, I think this is very close to what I need but I think I am having trouble with the terminology of “roles” vs. capabilities and how (URE) “User Role Editor” names things.

    I created a “Role” through URE and assigned it several capabilities
    Image1

    Using the this code

    add_action( 'edit_user_profile_update', 'my_save_extra_profile_fields' );
    
    function my_save_extra_profile_fields( $user_id ) {
    
    	if ( !current_user_can( 'edit_user', $user_id ) )
    		return false;
    	$user = new WP_User( $user_id );
    	$user->add_role('Volunteer User');
    
    }

    The user is not “added” to the “existing” role created by URE, instead a new Capability is listed in the custom capabilities.
    Image2

    So what I am trying to do is add the user to a Role (with capabilities).
    Image3

    Thank you again for any insight.

    Joe Dostie
    C2CND

    Plugin Author Vladimir Garagulya

    (@shinephp)

    Do not use role name, but use role ID

    $user->set_role('volunteer_user');

    change it if you used something else when added a custom role.
    WordPress shows role assigned to the user as a custom capability if it can not find such role.

    Thank you Vladimir,

    Sorry, I actually did use set_role I also tried a few other things and that was my last code I pasted.

    But I still seem to be having an issue and its really eluding me…

    First, I have checked URE and the roles I added. The name is Volunteer User and the ID is volunteer_user
    URE

    I also checked the database in the options table and usermeta table and the Role is s:14:”volunteer_user“;b:1;

    When I use this code:

    add_action( 'edit_user_profile_update', 'my_save_extra_profile_fields' );
    
    function my_save_extra_profile_fields( $user_id ) {
    	//if ( current_user_can( 'edit_user', $user_id ) )
    			$user = new WP_User( $user_id );
    			$user->set_role('volunteer_user');
    }

    When I click the “Update User” button on the user-edit.php page it basically looks like nothing happens, the user is never added to the “Volunteer User” role as I expect it to.

    So just out of curiosity I tried ‘contributor’ and still, nothing happened. At this point I thought the code was not working.

    But the weird part is the if I replace the role ID with something that does NOT exist, for example this code:

    add_action( 'edit_user_profile_update', 'my_save_extra_profile_fields' );
    
    function my_save_extra_profile_fields( $user_id ) {
    	//if ( current_user_can( 'edit_user', $user_id ) )
    			$user = new WP_User( $user_id );
    			$user->set_role('FAKE ROLE');
    }

    and then I click the “Update User” button, it creates the new capability and assigns the user that capability
    URE2
    So I know the function is firing.

    I have triple checked the spelling (all lower case)
    I disabled ALL plugins

    Everything I am reading says this is being done correctly.

    I’m sitting here scratching my head at this point.

    Kindest Regards

    PS.. that //if statement is NOT commented out (that was just another test)

    Phew…I got this working thanks to Chad Butler of WP-Members.

    What I ended up doing was a combination of his code and the WP_User.

    A leraning curve for me was that I thought everything on the user-edit.php could be controlled by the “edit_user_profile_update” hook but after Chad showed me that I needed a hook from his plugin, everything started working fine.

    Thanks for the help Vladimir!

    Plugin Author Vladimir Garagulya

    (@shinephp)

    Thanks for the information. You are welcome!

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘How can I set a role to the user programmatically?’ is closed to new replies.