To add a New WordPress Admin User w/ PHP you can tweak the database, but you can also simply do it with PHP code. I prefer the latter as I am better at PHP than MySQL and as messing with the database once a site is up is always tricky.

PHP Snippet

Here is a snippet from WP Scholar to achieve this as a WordPress Must Use Plugin:

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

As you can see it adds three variables:

It also makes sure to only add it when it exists and assigns it the role of administrator which is what we were aiming for.

Application

Just add it to the following directory:

wp-content/mu-plugins

If the mu plugins folder is missing add it. This setup loads it as a must use plugin that wil be loaded on the fly by WordPress. Once that is done you should be able to log in with your new details. Once you checked it worked do not forget to remove this file!

And there you have it. A new admin level user with a new password without touching existing users nor the database.

Leave a Reply

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