Create a new user automatically via functions.php in WordPress

I need to add a new user with administrator role through code, and I found this code:

add_action('init', 'add_user'); function add_user() { $username = 'username123'; $password = 'pasword123'; $email = ' drew@example.com '; // 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' ); } 

here

But when I added it to functions.php , I got this error:

  Fatal error: Call to a member function remove_role() on a non-object in ..../functions.php on line ... 

I also tried this code:

  function fb_wp_insert_user() { $user_data = array( 'ID' => '', 'user_pass' => wp_generate_password(), 'user_login' => 'dummy', 'user_nicename' => 'Dummy', 'user_url' => '', 'user_email' => ' dummy@example.com ', 'display_name' => 'Dummy', 'nickname' => 'dummy', 'first_name' => 'Dummy', 'user_registered' => '2010-05-15 05:55:55', 'role' => get_option('default_role') // Use default role or another role, eg 'editor' ); $user_id = wp_insert_user( $user_data ); } add_action( 'admin_init', 'fb_wp_insert_user' ); 

I changed the default role to adminstrator , but when I looked at users, I found this user without any role.

+9
php wordpress
source share
4 answers

This is your mistake

Fatal error: call member function remove_role () for an object without an object in .... / functions.php on line ...

This is because of the code $user->remove_role( 'subscriber' ); , and that means when you use the following code to retrieve a new user

 $user = get_user_by( 'id', $user_id ); 

It does not return a WP_User object. Thus, if you call a method on a non-object, this error appears, and this may be because you did not get the ID when you used

 $user_id = wp_create_user( $username, $password, $email ); 

You may not have created a user, in which case the return value may be object according to Codex

If successful, this function returns the user ID of the created user. In the event of a failure (username or email address already exists), the function returns an error object, with these possible values ​​and Messages;

empty_user_login; Unable to create user with empty login.

existing_user_login, This username is already registered.

existing_user_email, This email address is already registered.

SO, when you create a user, first check if the user exists or does not like

 add_action('init', 'add_my_user'); function add_my_user() { $username = 'username123'; $email = ' drew@example.com '; $password = 'pasword123'; $user_id = username_exists( $username ); if ( !$user_id && email_exists($email) == false ) { $user_id = wp_create_user( $username, $password, $email ); if( !is_wp_error($user_id) ) { $user = get_user_by( 'id', $user_id ); $user->set_role( 'administrator' ); } } } 

In addition, there is no need to renew and add the role, set_role ($ role) will delete the previous user roles and assign the user a new one. More about wp create user and get user in Codex . Also check wp_generate_password () to use a secure password instead of plain text.

Update:

add_user is a WordPress function, so change the name to something else, like add_my_user .

+11
source share

Make sure wp_create_user() actually created the user:

 add_action('init', 'add_user'); function add_user() { $username = 'username123'; $password = 'pasword123'; $email = ' drew@example.com '; $user = get_user_by( 'email', $email ); if( ! $user ) { // Create the new user $user_id = wp_create_user( $username, $password, $email ); if( is_wp_error( $user_id ) ) { // examine the error message echo( "Error: " . $user_id->get_error_message() ); exit; } // Get current user object $user = get_user_by( 'id', $user_id ); } // Remove role $user->remove_role( 'subscriber' ); // Add role $user->add_role( 'administrator' ); } 

Edited . The comments below show that the user has already been created. I updated the code to check this. (Essentially, now if the user does not already exist, it will be created.)

References

+6
source share
 $userData = array( 'user_login' => 'username', 'first_name' => 'First', 'last_name' => 'Last', 'user_pass' => 'password', 'user_email' => ' you@mail.com ', 'user_url' => '', 'role' => 'administrator' ); wp_insert_user( $userData ); 
+2
source share
 function kechweb_create_admin_account(){ $user = 'Username'; <br /> $pass = 'Password';<br /> $email = ' email@domain.com ';<br /> //if a username with the email ID does not exist, create a new user account<br /> if ( !username_exists( $user ) && !email_exists( $email ) ) {<br /> $user_id = wp_create_user( $user, $pass, $email ); <br /> $user = new WP_User( $user_id ); <br /> //Set the new user as a Admin <br /> $user->set_role( 'administrator' ); <br /> } } <br /> add_action('init','kechweb_create_admin_account'); 
0
source share

All Articles