plugins - Check if User exists in Wordpress Multisite

admin2025-04-19  0

Hi I am creating a custom Form to Register a new user in Backend. The problem is how I check if the user exists in another website of my Wordpress Network? And if not how can I register the new user?

Hi I am creating a custom Form to Register a new user in Backend. The problem is how I check if the user exists in another website of my Wordpress Network? And if not how can I register the new user?

Share Improve this question asked Oct 15, 2014 at 10:54 jobernasjobernas 1151 silver badge6 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 4

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',
        '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'] ) );
}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745073117a283406.html

最新回复(0)