BuddyDev

Search

bp_core_signup_user set a role

Tagged: 

  • Participant
    Level: Enlightened
    Posts: 53
    Jennifer on #27001

    Hi!

    I’m trying to create a user with a shop-manager role (not the default) without success. I tried add to my code this

    $new_role = ‘shop_manager’;
    $result = wp_update_user(array(‘ID’=>$user_id, ‘role’=>$new_role));

    But it didnt work. Any suggest? Thanks!!

    add_action('quform_post_process_7', function (array $result, Quform_Form $form) {
    if (function_exists('bp_core_signup_user')) {
        $username = $form->getValueText('quform_7_6');
        $email = $form->getValueText('quform_7_6');
         $password = $form->getValueText('quform_7_7');
    
        // XProfile fields
           $usermeta = array(
                 'field_1' => $form->getValueText('quform_7_10'),
                   //correo
                 'field_43' => $form->getValueText('quform_7_6'),
                 
            );
    
                 $usermeta['profile_field_ids'] = '1,43';
            $usermeta['password'] = wp_hash_password($password);
       
    
     bp_core_signup_user($username, $password, $email, $usermeta);
    
    }
    
        return $result;
    }, 10, 2);
  • Keymaster
    (BuddyDev Team)
    Posts: 24190
    Brajesh Singh on #27006

    Hi Jennifer,
    thank you for the question.

    BuddyPress overrides the roles on user account activation.

    Your best bet is to set role on ‘bp_core_activated_user’ action with priority 2 or lower.

    Regards
    Brajesh

  • Participant
    Level: Enlightened
    Posts: 53
    Jennifer on #27008

    Thank you Brajesh! is there a way to join it to a function to determine that role only for those new users? because I tried but it changes the role in the other registers
    Récords
    Jennifer

  • Keymaster
    (BuddyDev Team)
    Posts: 24190
    Brajesh Singh on #27009

    Hi Jennifer,
    The bp_core_signup_user returns the user_id of the newly created user. You can store a meta and use that at the time of activation to check.

    Hope that helps.

    Regards
    Brajesh

  • Participant
    Level: Enlightened
    Posts: 53
    Jennifer on #27017

    Hi Brajesh, thanks for your kindness.
    I thought I understood your explanation but something doesn’t work, can you see it where it failed? Thanks very much!!

    add_action('quform_post_process_7', function (array $result, Quform_Form $form) {
    if (function_exists('bp_core_signup_user')) {
        $username = $form->getValueText('quform_7_6');
        $email = $form->getValueText('quform_7_6');
        $password = $form->getValueText('quform_7_7');
    
        // XProfile fields
           $usermeta = array(
                 'field_1' => $form->getValueText('quform_7_10'),
                   //correo
                 'field_43' => $form->getValueText('quform_7_6'),
            );
    
                 $usermeta['profile_field_ids'] = '1,43';
            $usermeta['password'] = wp_hash_password($password);
       
    
     bp_core_signup_user($username, $password, $email, $usermeta, $user_id);
    update_user_meta( $user_id, 'shop_manager', $new_value );
    }
    
        return $result;
    }, 10, 2);
    
    function after_bp_activated_user($user_id, $key, $user) {
        $user = get_userdata($user_id);
        
        $role = get_user_meta($user_id, 'shop_manager');
        if ($role) {
            $user->set_role($role[0]);
        }
    }
    add_filter('bp_core_activated_user',  'after_bp_activated_user', 30, 2);
  • Keymaster
    (BuddyDev Team)
    Posts: 24190
    Brajesh Singh on #27025

    Hi Jennifer,
    Thank you for sharing the code.

    I have updated it slightly and it should work.

    
    
    add_action( 'quform_post_process_7', function ( array $result, Quform_Form $form ) {
    	if ( ! function_exists( 'bp_core_signup_user' ) ) {
    		return;
    	}
    	$username = $form->getValueText( 'quform_7_6' );
    	$email    = $form->getValueText( 'quform_7_6' );
    	$password = $form->getValueText( 'quform_7_7' );
    
    	// XProfile fields
    	$usermeta = array(
    		'field_1'  => $form->getValueText( 'quform_7_10' ),
    		//correo
    		'field_43' => $form->getValueText( 'quform_7_6' ),
    	);
    
    	$usermeta['profile_field_ids'] = '1,43';
    	$usermeta['password']          = wp_hash_password( $password );
    
    	$user_id = bp_core_signup_user( $username, $password, $email, $usermeta );
    	if ( $user_id ) {
    		update_user_meta( $user_id, '_is_shop_manager', 1 );
    	}
    
    	return $result;
    }, 10, 2 );
    
    function after_bp_activated_user( $user_id, $key, $user ) {
    	$user = get_userdata( $user_id );
    
    	$role = 'shop_manager';// please changew ith correct role.
    
    	$is_shopmanager = get_user_meta( $user_id, '_is_shop_manager', true );
    	if ( $is_shopmanager ) {
    		$user->set_role( $role );
    	}
    }
    
    add_action( 'bp_core_activated_user', 'after_bp_activated_user', 30, 2 );
    
    

    Please make sure to use correct role in the 2nd function. Also, It will only work on non multisite installation as on multisite ‘bp_core_signup_user’ does not return the user id correctly(currently).

    Regards
    Brajesh

  • Participant
    Level: Enlightened
    Posts: 53
    Jennifer on #27027

    Hi! i used your code, but I can’t change the user’s role.
    the current default role is *subscriber* and the one that has to be is * shop_manager *
    i tried with a priority different like:

    add_action( 'bp_core_activated_user', 'after_bp_activated_user', 10, 2 );
    and replace in the code
    $user->set_role( $role );

    for this:

    `$user = new WP_User( $user_id );
    $user->remove_role( ‘subscriber’ );
    $user->add_role( ‘shop_manager’ );`

    but I couldn’t do it yet.
    do u have any suggestions?
    Thanks very much!

    • This reply was modified 4 years, 4 months ago by Jennifer.
    • This reply was modified 4 years, 4 months ago by Jennifer.
  • Participant
    Level: Enlightened
    Posts: 53
    Jennifer on #27030

    Hi Brajesh
    I’m trying differently, it dont works ..a validation with a xprofile field

    
    add_action( 'quform_post_process_7', function ( array $result, Quform_Form $form ) {
    	if ( ! function_exists( 'bp_core_signup_user' ) ) {
    		return;
    	}
    	$username = $form->getValueText( 'quform_7_6' );
    	$email    = $form->getValueText( 'quform_7_6' );
    	$password = $form->getValueText( 'quform_7_7' );
    
    	// XProfile fields
    	$usermeta = array(
    	                //name
                 'field_1' => $form->getValueText('quform_7_28'),
    		'field_43' => $form->getValueText( 'quform_7_6' ),
                                  'field_59' => 'Inmobiliaria',
                  
    	);
    
    	$usermeta['profile_field_ids'] = '1,43,44,59';
    $usermeta['password']          = wp_hash_password( $password );
    	$user_id =    bp_core_signup_user($username, $password, $email, $usermeta);
    
    	return $result;
    }, 10, 2 );
    
    function after_bp_activated_user( $user_id, $key, $user ) {
    global $bp;
    
    $user = get_userdata( $user_id );
    $role='shop_manager';
    $profile_stage = xprofile_get_field_data('59');
    if ('Inmobiliaria' == $profile_stage){
    		$user->set_role( $role );
    	}
    }
    add_action( 'bp_core_activated_user', 'after_bp_activated_user', 30, 2 );
    
    • This reply was modified 4 years, 4 months ago by Jennifer.
  • Keymaster
    (BuddyDev Team)
    Posts: 24190
    Brajesh Singh on #27032

    Hi Jennifer,
    How does the user activate their account?

    The code applies role on account activation. Please let me know how it works and I will be able to assist.,

    Regards
    Brajesh

  • Participant
    Level: Enlightened
    Posts: 53
    Jennifer on #27033

    Hi Brajesh
    The user answers the form and automatically connects to his profile. I had not thought about this :(.. there is no activation .. can I use another hook?
    Thanks very much

You must be logged in to reply to this topic.

This topic is: not resolved