Sometimes you have access to a WordPress site’s files or File Manager, but you do not have administrator login access to the WordPress dashboard.
In that situation, you can temporarily create a new admin user by adding a small snippet to your theme’s functions.php file. See the code below. After adding the code and saving the file, visit the website once in your browser. Now, an admin user will be created in WordPress.
This method is quick, simple, and uses native WordPress functions.
Important Security Note
This code should only be used temporarily and only on websites you own or manage.
After the admin account is created:
- Remove the code immediately
- Change the password to something secure
- Avoid leaving temporary admin creation code on production sites
Pro Tip
You can also place this code inside a small custom plugin instead of the theme file if you prefer a cleaner approach.
add_action( 'init', 'create_temporary_admin_user' );
function create_temporary_admin_user() {
$username = 'newadmin';
$password = 'StrongPassword123!';
$email = 'admin@example.com';
if ( ! username_exists( $username ) ) {
$user_id = wp_create_user(
$username,
$password,
$email
);
$user = new WP_User( $user_id );
$user->set_role( 'administrator' );
}
}