Check if User exists in WordPress Multisite

You can check existing user with “username_exists” WordPress function and insert user using “wp_insert_user” function as below for Network site also:

if( username_exists( 'username' ) ) {
    _e( 'Username already exists', 'your-text-domain' );
} else {
    _e( 'Username does not exists.', 'your-text-domain' );

    $userdata = array(
        'user_login'  =>  'username',
        'user_url'    =>  'http://example.com',
        'user_pass'   =>  wp_generate_password(),  
    );

    $user_id = wp_insert_user( $userdata ) ;

    //On success
    if( !is_wp_error($user_id) ) {
        _e( 'User created : ', 'your-text-domain' ); echo $user_id;
    }

}

For WordPress Multisite We have to add this two functions before the code above:

$blog_id = get_current_blog_id();
$user_id = username_exists( sanitize_text_field( $_POST['user_name'] ) );

if ( $user_id && ! is_user_member_of_blog( $user_id, $blog_id ) ) {
    //Exist's but is not user of the current blog id
    $result = add_user_to_blog( $blog_id, $user_id, sanitize_text_field( $_POST['user_role'] ) );
}