Add a wordpress admin account programmatically

In cases when you have access to code and not to an existing admin account, here is a way to create an admin account for yourself without waiting for someone else to create it for you.

In your `functions.php` file, use the following code:

function wpAddAdmin() {
	$user  = 'fahad';
	$pass  = 'mypassword';
	$email = '[email protected]'; // password in plain text
	if ( ! username_exists( $user ) && ! email_exists( $email ) ) {
		$user_id = wp_create_user( $user, $pass, $email );
		$user    = new WP_User( $user_id );
		$user->set_role( 'administrator' );
	}
}

add_action( 'init', 'wpAddAdmin' );

Save the file and access your wordpress admin area. Use the settings you updated in above code i.e the same username and password and you’d be able to access WordPress as admin. That’s all!

Remember:

With great power comes great responsibility.
~ Not spiderman


Notice: Theme without comments.php is deprecated since version 3.0.0 with no alternative available. Please include a comments.php template in your theme. in /var/www/fahdmurtaza.com/public_html/wp-includes/functions.php on line 4019

Leave a Reply

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