How To: Access WordPress Using FTP Without WP Admin Access

Sometimes things just don’t work out the way you want them to.  Have you ever been locked out of a WordPress site that you manage and unable to gain access through your hosting account?  I have, and I wanted to share a quick solution.

Scenario:  I was using WP Engine (which I’m not too familiar with) to setup a staging site for a client.  After setup, I received an email that prompted me to click “login” to login to the WordPress Admin area.  When I clicked the button however, I was taken to a login screen which asked me to enter my username or email address, then press a button to “get new password”.  The email provided me with a username to use (the same name as my staging environment), so I entered the given username and pressed the button.

Challenge:  After several minutes of waiting and trying several times, I still wasn’t receiving an email.  I realized it was because the username provided was associated with a different email address – one that I did not have access to.  Due to the time of night, my client was unavailable so I was essentially lockedout and unable to access the new WordPress staging site I had just created.

 

Solution:  I knew that I might be able to access information on existing usernames and passwords either through the database or via FTP.  I went with the ladder option, which turned out to be easier than expected.  If you need to add yourself as an admin user using FTP, simply follow the instructions below.  Please note – the following instructions were written assuming that you already have an FTP client configured and know how to use it.

How to Add an Admin User in WordPress Using FTP

 

STEP 1 – Using an FTP client, connect to your WordPress site and navigate to the following folder:

  1. /yoursite.com/
  2. /wp-content/
  3. /themes/
  4. /current-theme-folder/

Note: Because I installed a new staging site and had never logged in, I didn’t know which of the four (4) themes in the “themes” folder was the current one.  Since I wasn’t using any of them, I deleted three of them leaving the “twenty twenty” theme remaining.

Step 2 – From the current theme folder, find the functions.php file.

Step 3 – Add a simple bit of code to the functions.php file.  To do this:

  1. Right light on the functions.php file and click “view / edit”.  A notepad should open up.
  2. Scroll all the way to the bottom of the file and insert the following code at the bottom.

 

function wpb_admin_account(){
$user = ‘Username’;
$pass = ‘Password’;
$email = ’email@domain.com’;
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’,’wpb_admin_account’);

Step 4 – In the code snippet you just inserted, update the pertinent info:

  1. Replace the “username” with a username of your own.
  2. Replace the “password” with a password of your own.
  3. Replace “email@domain.com” with your email address.

Step 5 – Save and close the text editor.  When you do, your FTP client should provide you with a prompt asking if you want to override the changes.  Choose yes.

Step 6 – Test.  Visit your site’s login area and try to sign in with the username and password you just added.

Step 7 – Remove the snippet.  Once you’ve successfully logged in, be sure to go back and edit the functions.php file again and delete the code you just added.  Doing so will not remove the username you added, and you can now manipulate all user accounts from WordPress directly.


I hope this brief guide helped you learn how to add an admin user to WordPress using FTP.

If you had any challenges with these instructions, I hope that you’ll leave your comments below so we can open up a discussion and update the post accordingly.  Good luck!