A few times I’ve gotten into a situation where I have access to WordPress files (via SFTP or other means) but I’m otherwise locked out of the site (my user hasn’t been created or was accidentally deleted). In those cases, the quickest option is generally just to create a new admin user with code.

If I know which theme is active, I’ll generally just drop this code into the bottom of functions.php for that theme, refresh the site once or twice in the browser, and then delete the code (important: make sure to delete it!)

<?php
add_action( 'init', function () {
$username = 'admin';
$password = 'password';
$email_address = 'webmaster@mydomain.com';
if ( ! username_exists( $username ) ) {
$user_id = wp_create_user( $username, $password, $email_address );
$user = new WP_User( $user_id );
$user->set_role( 'administrator' );
}
} );

If you don’t know which theme is active, you can also create a new directory called “mu-plugins” inside wp-content, and paste this code into a file called create-admin-user.php as WP Scholar recommends.

Posted by:Devin

I'm a WordPress developer based in Austin, Texas. I run a little theme shop called DevPress and work for a startup called Nano. Find me on twitter @devinsays.

Leave a Reply

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