您可以使用“”检查现有用户username_exists“WordPress函数和插入用户使用”wp_insert_user“网络站点的功能如下:
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;
}
}
对于Wordpress Multisite,我们必须添加这两个函数
before 上述代码:
$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\'] ) );
}