The WPFolks app is out on iPhone and Android. Free, no ads. Download on the App Store Get it on Google Play
Skip to main content
Login Join
Snippet · PHP

Create a Temporary WordPress Admin User Using Code

Shared by Krupal Panchal · June 1, 2026 · @init

3 copies
77 views
Back to Snippets

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:

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' );
	}
}
Know a different way to do this? Add your approach as a variation so folks can compare them side by side.
Submit a variation

0 comments