Tagged: profile
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);
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
BrajeshHi 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
BrajeshHi 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);
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
BrajeshHi! 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!Hi Brajesh
I’m trying differently, it dont works ..a validation with a xprofile fieldadd_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, 11 months ago by Jennifer.
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
You must be logged in to reply to this topic.