Insert a WordPress Admin user with PHP code

You might find yourself once in a while in a scenario where you have FTP access, but no WordPress admin access to login from the dashboard. Well, adding an admin is as easy as copy-paste.

Step 1:

Identify the theme that the site is using. To do so, load the website in a browser and view the source code, search the document for /wp-content/themes. The theme name will be directly after the /themes folder

Step 2:

FTP into the site, and browse to the functions.php file found at the root of the active theme

Step 3:

Paste this snippet of code, and change the username, password and email address to your own preferred ones.

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' );
	}
	
} );

Step 4:

Load any page on the site. Once you load a page, the code will run, and you will be added as an admin, and you can go ahead and login right away

Step 5:

Delete the code and you’re done!

 

Looking for the ultimate free theme? Check out Buildr

Leave a Reply