Answer the question
In order to leave comments, you need to log in
How to update the user's role by condition at the time of registration?
Please tell the wordpress amateur how to update the user role depending on the meta data passed at the time of registration.
I wrote this code but it doesn't work.
add_action( 'user_register', 'my_user_registration' );
function my_user_registration( $user_id ) {
$meta_com = get_user_meta( $user_id, 'user_type', true );
$value_yes = 'yes';
$role_yes = 'company';
$role_no = 'customer';
if ( $meta_com == $value_yes) {
wp_update_user( array( 'ID' => $user_id, 'role' => $role_yes ) );
} else {
wp_update_user( array( 'ID' => $user_id, 'role' => $role_no ) );
}
}
Answer the question
In order to leave comments, you need to log in
Try like this:
add_action('user_register', 'my_user_registration');
function my_user_registration($user_id)
{
$meta_com = get_user_meta($user_id, 'user_type', true);
$user = new WP_User($user_id);
$value_yes = 'yes';
$role_yes = 'company';
$role_no = 'customer';
if ($meta_com == $value_yes) {
$user->add_role($role_yes);
} else {
$user->add_role($role_no);
}
}
Already found a solution!
add_action( 'woocommerce_created_customer', 'my_user_registration' );
function my_user_registration( $user_id ) {
$meta_com = get_user_meta( $user_id, 'user_type', true );
$value_yes = 'yes';
$role_yes = 'company';
$role_no = 'customer';
if ( $meta_com == $value_yes) {
wp_update_user( array( 'ID' => $user_id, 'role' => $role_yes ) );
} else {
wp_update_user( array( 'ID' => $user_id, 'role' => $role_no ) );
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question