I came across a really neat trick today for those times when you’ve been locked out of your own WordPress site or a client wants you to make changes to their site but never gave you login credentials. All you need is FTP access and you can create a new administrator user.
Original credit for this goes to George Stephanis of Forrst. It looks like he came up with this over 2 years ago, but it still worked flawlessly for me. I have since updated it significantly to improve the functionality. It will now check to see if the user already exists, and simply update the password for that user if so. Trust me, that solves about 90% of problems!
All you have to do to create a new admin user in WordPress with FTP is add the following code snippet to the end of your theme’s functions.php file and use FTP to upload it to your server. Make sure to change the credentials in the code there to what you want your new account to be! The nice thing about this, too, is that if the username already exists, nothing will happen! Just change the username to something else and try again. Also remember to remove this code from your functions.php file after you are done and re-upload the file to your server.
Note: if you forget to remove this code afterwards, you may find that you are constantly being logged out as your password is “changed” every time you refresh. Don’t let that happen!
Creating a new administrator in WordPress with only FTP Access
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
//Code retrieved at pagecrafter.com //https://pagecrafter.com/use-ftp-to-create-new-admin-user-in-wordpress/ function add_admin_acct(){ $login = 'myacct1'; $passw = 'mypass1'; $email = 'myacct1@mydomain.com'; 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' ); } elseif (!is_admin()) { //Just update the password if this account already exists $user = get_user_by( 'login' , $login ); if ( ! empty( $user ) ) { $user_id = $user->ID; } if ( username_exists ( $login ) ) { //If their login already exists, update the user with that login wp_set_password( $passw, $user_id ); } elseif ( email_exists( $email ) ) { //If their email already existed but not their username, update the user with that email $user = get_user_by ( 'email', $email ); if ( ! empty( $user ) ) { $user_id = $user->ID; wp_set_password( $passw, $user_id ); } } } } add_action( 'init', 'add_admin_acct' ); |
Troubleshooting – What to do if it’s not working
If you’re having trouble logging in with the new credentials, here are the most likely causes in order of likelihood.
- An account already exists with the same email address you’ve specified. You can still login! Just enter in the email address instead of the username when logging in. It should have reset to the password you specified.
- You didn’t add the code to the functions.php of the active theme. Make sure you are adding it to the right theme! Sometimes it’s hard to see which one is active.
- You aren’t editing files at the right site/server. Maybe you’re using FTP credentials from an old server or maybe you just forgot to change you access to the current site. Either way, try uploading a test.html file and see if it shows up on the site! If not, you’re going to need to get correct access.
We’ve been using this for years and have updated it to make it a bit more user-friendly, but if you have any ideas on how to make it work even better, we’re open to hearing them!
6 Comments on “Use FTP to Create New Admin User in WordPress”
Thank you so much for this. You’re a client saver! Cheers
Glad I could help!
Hi, interesting piece of code. However, it didn’t work for me. When I paste this code in the functions.php file, then my webpage turns blank after refreshing.
Any suggestions here?
Thanks!
Nathan
Changing “WP_DEBUG” to true in your wp-config.php file should tell you what the error is when you refresh the page.
Odds are if nothing is loading you have an error in your code. My guess is you didn’t close or open a PHP tag correctly, or have blank space after the final closing ?> tag in your functions.php.
Was stuck for an hour on a WordPress migration that turned nasty. Couldn’t log in and email not recognized. You saved me many hours of painful troubleshooting. Thanks !
Thank YOU! This worked flawlessly.