User 19 is a WP Admin.
$user = new WP_user( 19 );
$user->add_role( 'custom_role' );
The above code works and adds the custom_role
role.
However, when I run the same code on the personal_options_update
and edit_user_profile_update
hook it does not.
I have added a checkbox to the user profile page. The function called from both the above hooks then...
if( user_can( $user_id, 'administrator' ) ) {
$user = get_user_by( 'id', $user_id );
if( ! empty( $_POST['my_checkbox'] ) ) {
update_user_meta( $user_id, 'my_checkbox', true );
$user->add_cap( 'one_cap' );
$user->add_cap( 'two_cap' );
$user->add_role( 'custom_role' );
}
}
one_cap
and two_cap
are both set correctly for the user, but no sign of custom_role
and nothing written to the debug log.
What could be the cause of this?