I have the following code, which properly adds the wp_user_level of 2 when a new user is registered with the role "division_manager". I need the wp_user_level to be non-zero so they can be the author of posts.
add_action( 'user_register', 'set_user_default_division_manager', 10, 1 );
function set_user_default_division_manager( $user_id ) {
$user = new WP_User( $user_id );
foreach( $user->roles as $role ) {
if( $role === 'division_manager' ) {
update_user_meta( $user_id, 'wp_user_level', 2 );
}
}
}
I'm trying to work out code that will add the wp_user_level when an existing user is given the role of "division_manager". This is what I have, but the user level only updates when I save the users profile twice. I've also tried the actions: 'personal_options_update' & 'edit_user_profile_update'.
add_filter( 'insert_user_meta', 'update_division_manager_user_level', 10, 1 );
function update_division_manager_user_level( $user_id ) {
foreach( $_POST['members_user_roles'] as $role ) {
if( $role === 'division_manager' ) {
update_user_meta( $user_id, 'wp_user_level', 2 );
}
}
}
Any idea what I'm missing?