The problem is that you're using get_current_user_id();
. There's two reasons this is incorrect:
- The user being created is not necessarily the current user. For example, if an admin creates the user manually.
user_register
runs immediately after the user is created, but before the user is logged in. So when user_register
runs for somebody registering on the front end, there is no current user.
If you want to add meta to the user being created, when they are created, you need to use the $user_id
variable that's passed to the callback function. This user ID is for the user that was created, regardless of whether or not they are the currently logged in user.
function add_to_group( $user_id ) {
update_user_meta( $user_id, 'learndash_group_users_6597', '6597' );
}
add_action( 'user_register', 'add_to_group' );
Note that the $user_id
passed to add_to_group
is what we are using in update_user_meta()
.
Also, in your original code you were accepting 3 arguments to the function. I removed them because as you can see from the documentation for the hook, only 1 variable, $user_id
is passed to the callback.