Skip to:

Update user Role?

  • psmorrow


    Hi, I’ve been trying to update the logged in user’s role based on other profile info, but can’t get it working:

    function update_user_role () {
    	$member_type = xprofile_get_field_data( 'Member Type', bp_loggedin_user_id());	
    	if($member_type == 'Option 2' ) {
    		$user_id = bp_loggedin_user_id();
    		$new_value = 'author';
    		bp_update_user_meta( $user_id, 'role', $new_value );
    add_action( 'bp_ready', 'update_user_role',301);

    Any suggestion? Many thanks!

Viewing 3 replies - 1 through 3 (of 3 total)

  • danbp



    guess you confuse member types and user roles. MT where introduced to allow site owners to “categorize” their users. WP user role is slightly different, as it defines what user can do on a site, a kind of access management. So in brief, roles are independent from BP member types.

    MT and roles are also stored in different tables: MT in wp_terms and roles in wp_usermeta – with the denomination of wp_capabilities…

    By default, roles are automatically attibuted at registration level or manually by a site admin. Member types follow the same logic, under condition the xprofile component is activate.

    Now the question is: what do you want to do exactly after registration ? Change the role or change the type ?

    Roles can be affected from the admin user list page. I would recommand to not modify this behave at registration level, to avoid abuse by promoting fake user for example.

    MT can be changed from the xprofile admin interface. There is also this plugin for a more in depth management.

    Using a custom function is of course possible. But just to clarify, the one you give as example let’s suppose you have a need for bulk action over user status. Automated task is well and good, but in this case, why not attribute a higher role or type to all by default ?
    And if it is not intended for “all” (what i suppose is the case), do you have so many exception that you need such a function ? Can’t you do that manually ? Just my 2 cents. 😉



    Thanks for the explanation and insight danbp! I am not married to updating the user role with a function. The only reason I wanted to do it is because I’m using the events manager plugin and I don’t want basic members to have permissions that are determined using the role. And I’m open to other options….

    If i update the role with a function, I can avoid hiding a bunch of things using functions…which is easy enough….but my thinking was that updating the role was more efficient.

    I do want to do this automatically, because I want to grant users privileges during registration.

    Which do think is a better option: hiding a bunch of screen elements with a function based on the member_type attribute I created; or, updating the role at registration and have these element hidden automatically using the Events Manager plugin?

    Many thanks again!



    been thinking more about it, and I really hope to update the user role with a custom function. I can’t figure out why the code above won’t do this? Any suggestions you have are greatly appreciated!

Viewing 3 replies - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.
Skip to toolbar