When I’m unable to log into Wordpress I use one of these two techniques to add a new user to WordPress.
Technique #1
Adds a user to WordPress via FTP. This one’s been known as far back as 2011. George Stephanis posted an article on the subject, and it still remains relevant today.
Simply add the below code to your active WordPress theme functions.php file, then visit the site to inject the new user and password into the database.
This will instantly create a new admin user.
Just remember to remove the below code from your functions.php file once you’ve verified the new username and password is working nicely.
function add_admin_acct(){ $login = 'yourusername'; $passw = 'yourpass'; $email = 'youremail'; 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' ); } } add_action('init','add_admin_acct');
Obligatory disclaimer: Make a backup of your database beforehand. Oopsy!
Screen capture example (code above inserted in yellow below):
The result after adding the code above to the functions.php. A new user created:
Technique #2
Adds a user to WordPress via phpMyAdmin.
A method I use when phpMyAdmin is available. Simply log into phpMyAdmin and click on the database name shown in the left column.
Not sure what database your WordPress installation uses?
View the line within your wp-config.php file that looks like this:
define(‘DB_NAME’, ‘wp_mydb1’);
Along the top of the page you’ll see a line of tabs that look like this:
Just click on the SQL tab, and paste the below text into the box.
INSERT INTO `wp_users` (`user_login`, `user_pass`, `user_nicename`, `user_email`, `user_status`) VALUES ('yourname', MD5('mysecretpassword'), 'firstname lastname', 'you@your-domain.com', '0'); INSERT INTO `wp_usermeta` (`umeta_id`, `user_id`, `meta_key`, `meta_value`) VALUES (NULL, (Select max(id) FROM wp_users), 'wp_capabilities', 'a:1:{s:13:"administrator";s:1:"1";}'); INSERT INTO `wp_usermeta` (`umeta_id`, `user_id`, `meta_key`, `meta_value`) VALUES (NULL, (Select max(id) FROM wp_users), 'wp_user_level', '10');
With text entered and edited click the GO button to create a new user.
You probably should change the text in red before slamming that GO button.
Then log into WordPress as usual with your newly added yourname and mysecretpassword.
That’s it – Enjoy!
Looking for other options to change your WordPress password?
See the HackGuard.com how-to, “How do I reset my WordPress password?”
10 Comments
Ghilaine says
Hi,
I’ve tried but it doesn’t work.
That is what I’ve pasted in the functions.php file :
function add_admin_acct(){
$login = ’ghilaine’;
$passw = ‘&c&o(le’;
$email = ‘ghilaine.coulon@orange.fr’;
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’ );
}
}
add_action(‘init’,’add_admin_acct’);
As my user name is ghilaine and password is &c&o(le
Jim Walker says
I use this quite often.
Try this code just below the starter PHP tag:
function add_admin_acct(){
$login = ‘yourname’;
$passw = ‘yourpass’;
$email = ‘youremail’;
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’ );
}
}
add_action(‘init’,’add_admin_acct’);
Oliver says
Everytime I try to add the code either to the themes FTP or doing the /mu-plugins way I get this error.
Fatal error: Call to undefined function username_exists() in /homepages/17/d221539452/htdocs/SOUTHERNMOSTGUNSCOM/blog/wp-content/mu-plugins/functions.php on line 8
any idea what I’m doing wrong?
Jim Walker says
Hi,
This code only goes into the functions.php for the few minutes needed to add the new account.
* Recommend you remove the code immediately after verifying the new user and password is working.
Oliver says
Yes I added it to functions.php and when I go over to the website log in screen type that info in I get an error :/
Jim Walker says
Hmm, that normally only occurs if you added it before the starting PHP tag. You sure you copy / pasted the text, right below the first:
Oliver says
Here is where I am putting it. Right at the top. (wit my own credentials)
http://pastebin.com/cDiWTxSi
I’ve tried at the bottom and the /mu-plugins way neither seems to work. Not sure if that helps at all. I am not much of a PHP guy so my knowledge on this is limited.
Jim Walker says
I don’t see why this would not work Oliver. Just email direct, jim@
and I’ll see what I can do to assist. No need to use this slow form. 🙂
Mitchell Miller says
Hello Jim:
You do not need PHP to create a user.
Try WP-CLI: `wp user create `
Best wishes,
Mitchell
Jim Walker says
For those who have WP-CLI that’s a nice option.
Most folks have no idea what you are talking about though–so the simplest solution, adding code to functions.php.
Thank you for the tip!
+++
You are super smart