wp_insert_user if user exists

When updating a user with:

$user_id = wp_update_user( $userdata );

you need to have the user ID set within $userdata.

Also make sure to validate the $user_id output of wp_update_user() as it can be either an integer or an instance of WP_Error, before using it in update_user_meta().

One can use is_wp_error( $user_id ) to check if $user_id is an instance of the WP_Error class.

PS:

Make sure to have a look at PHP errors while developing.

When the new fatal error protection (WSOD) kicks in after 5.1, one might need to disable it by defining the constant:

WP_DISABLE_FATAL_ERROR_HANDLER 

as true in wp-config.php on dev installs.