Create new user automatically via functions.php in WordPress

Check to make sure that wp_create_user() actually created the user:

add_action('init', 'add_user');
function add_user() {
    $username = 'username123';
    $password = 'pasword123';
    $email = '[email protected]';

    $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: Per the comments below, it appears that the user has already been created. I've updated the code to check for that. (In essence, now, if the user doesn't already exist, it'll be created.)

References

  • get_user_by()
  • wp_create_user()

This is your error

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

It's is because of $user->remove_role( 'subscriber' ); code and it means that, when you are using following code to retrieve the new user

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

It's not returning a WP_User object. So, if you call a method on a non object, this error shows up and it could be because you didn't get an ID when you used

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

It's possible that, you didn't successfully created a user and in this case the return value could be an object according to Codex

When successful - this function returns the user ID of the created user. In case of failure (username or email already exists) the function returns an error object, with these possible values and messages;

empty_user_login, Cannot create a user with an empty login name.

existing_user_login, This username is already registered.

existing_user_email, This email address is already registered.

SO, when you are creating a user, at first check if the user exist or not like

add_action('init', 'add_my_user');
function add_my_user() {
    $username = 'username123';
    $email = '[email protected]';
    $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' );
        }
    }
}

Also, there is no need for reomving and adding the role, set_role($role) will remove the previous roles of the user and assign the user the new one. Read more about wp create user and get user by on Codex. Also, check the wp_generate_password() to use a secured password instead of plain text.

Update :

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

Tags:

Php

Wordpress