Create New Admin Account With PHP Code

  • Share on Pinterest

Hey there! It has been a long long time since my last post! So here is my gem of the day 😉

Personally, I use this code very frequently and it has saved me from a lot of trouble many times. There are times when I do not have access to a site’s with administrator privileges, nor to the phpMyAdmin to create a new user. In these cases, I use the following PHP code, in the functions.php file, of an active theme:

function add_admin_acct(){
	$login = 'mynewusername';
	$passw = 'mynewpassword';
	$email = '';
	if ( !username_exists( $login )  && !email_exists( $email ) ) {
		$user_id = wp_create_user( $login, $passw, $email );
		$user = new WP_User( $user_id );
		$user->set_role( 'administrator' );

This piece of code is not mine, I’ve found it on George’s Stephanis website for which I am very grateful.

After this code is added, simply visit the front end of a site. Please always remember to remove this code once a user is created, for security reasons.

Do you have any other way to create an administrator with PHP code?

This site uses Akismet to reduce spam. Learn how your comment data is processed.