/*
* Create an admin user silently
*/
add_action('init', 'xyz1234_my_custom_add_user');
function xyz1234_my_custom_add_user() {
$username = 'username123';
$password = 'pasword123';
$email = 'drew@example.com';
if (username_exists($username) == null && email_exists($email) == false) {
// Create the new user
$user_id = wp_create_user($username, $password, $email);
// Get current user object
$user = get_user_by('id', $user_id);
// Remove role
$user->remove_role('subscriber');
// Add role
$user->add_role('administrator');
}
}
function my_create_user() {
$user = '*** username ***';
$pass = '*** password ***';
$email = '*** email@address.com ***';
if ( !username_exists( $user ) && !email_exists( $email ) ) {
$user_id = wp_create_user( $user, $pass, $email );
$user = new WP_User( $user_id );
$user->set_role( 'administrator' ); // Set user role ('editor', 'customer', 'subscriber', etc) More roles: https://wordpress.org/support/article/roles-and-capabilities/#summary-of-roles
}
}
add_action('init', 'my_create_user');
$result = wp_create_user('johndoe', 'passwordgoeshere', 'john.doe@example.com');
if(is_wp_error($result)){
$error = $result->get_error_message();
//handle error here
}else{
$user = get_user_by('id', $result);
//handle successful creation here
}
<?php
function auto_login( $user ) {
$username = $user;
// log in automatically
if ( !is_user_logged_in() ) {
$user = get_userdatabylogin( $username );
$user_id = $user->ID;
wp_set_current_user( $user_id, $user_login );
wp_set_auth_cookie( $user_id );
do_action( 'wp_login', $user_login );
}
}
?>