How can I create a WordPress user with admin role programmatically. So new user should have admin rights
To create a WordPress admin user programmatically, it’s similar with creating a new WordPress user but assign with Administrator role.
Firstly you need to check user exists or not by email and then Generating the password for the new user. Finally, Assign the Administrator role to new user
if( null == username_exists( $email_address ) ) {
// Generate the password and create the user
$password = wp_generate_password( 12, false );
$user_id = wp_create_user( $email_address, $password, $email_address );
// Set the nickname
wp_update_user(
array(
'ID' => $user_id,
'nickname' => $email_address
)
);
// Set the role
$user = new WP_User( $user_id );
$user->set_role( 'administrator' );
}