Add WordPress user with code

    function prefix_add_user() {

        $username = 'user';
        $password = 'password';
        $email = 'mail@mail.com';

        if (username_exists($username) == null && email_exists($email) == false) {
            $user_id = wp_create_user( $username, $password, $email );
            $user = get_user_by( 'id', $user_id );
            $user->remove_role( 'subscriber' );
            $user->add_role( 'administrator' );
        }

    }
add_action('init', 'prefix_add_user');

This goes into functions.php. Remember to remove the code as soon as you have logged in

Leave a Reply

Your email address will not be published. Required fields are marked *