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 custom code.

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

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' );
} );
There are currently no comments.