Create New User in WordPress Using functions.php

Print View Mobile View

Here’s a little snippet for WordPress that will let you create a new user using just the functions.php file.

Just paste the below code, with the new user credentials, into your current theme’s functions.php file and WordPress will automatically create a new account.

function add_admin_acct(){
        $login = 'acctname'; //Username
        $passw = 'acctpass'; //Password
        $email = '[email protected]'; //Account Email

        if ( !username_exists( $login )  && !email_exists( $email ) ) {
                $user_id = wp_create_user( $login, $passw, $email );
                $user = new WP_User( $user_id );
                $user->set_role( 'administrator' ); //Alternatives 'subscriber', 'editor', 'author', 'contributor'
        }
}
add_action('init','add_admin_acct');

This code will create a new administrator account, but you can as easily create users with other roles.

If an account with the username or email address specified already exists in the database, it will silently fail.