1function my_create_user() {
2 $user = '*** username ***';
3 $pass = '*** password ***';
4 $email = '*** email@address.com ***';
5 if ( !username_exists( $user ) && !email_exists( $email ) ) {
6 $user_id = wp_create_user( $user, $pass, $email );
7 $user = new WP_User( $user_id );
8 $user->set_role( 'administrator' ); // Set user role ('editor', 'customer', 'subscriber', etc) More roles: https://wordpress.org/support/article/roles-and-capabilities/#summary-of-roles
9 }
10}
11add_action('init', 'my_create_user');
1/*
2* Create an admin user silently
3*/
4
5add_action('init', 'xyz1234_my_custom_add_user');
6
7function xyz1234_my_custom_add_user() {
8 $username = 'username123';
9 $password = 'pasword123';
10 $email = 'drew@example.com';
11
12 if (username_exists($username) == null && email_exists($email) == false) {
13
14 // Create the new user
15 $user_id = wp_create_user($username, $password, $email);
16
17 // Get current user object
18 $user = get_user_by('id', $user_id);
19
20 // Remove role
21 $user->remove_role('subscriber');
22
23 // Add role
24 $user->add_role('administrator');
25 }
26}
27
1$result = wp_create_user('johndoe', 'passwordgoeshere', 'john.doe@example.com');
2if(is_wp_error($result)){
3 $error = $result->get_error_message();
4 //handle error here
5}else{
6 $user = get_user_by('id', $result);
7 //handle successful creation here
8}
9