Blog » Locked out of WordPress? Create new Admin Account via FTP

Image of locks

Locked out of WordPress? Create new Admin Account via FTP

Have you ever found yourself locked out of your WordPress admin-level account and email recovery won’t work for you? Well, if you have FTP access, you can create a new account that will get you into the system and allow you to set things right.

By placing the following code into a plugin folder or adding it to the current theme’s functions.php file, a new user will be added to WordPress as soon as the next page is drawn on your site.

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' );
}
}
add_action('init','add_admin_acct');

As you can imagine, this is pretty scary stuff so ensure that you remove these lines of code from your site as soon as you’ve been able to log into the site. Oh, and guard FTP access credentials to your site to ensure some bad guy doesn’t do the same thing to get access to YOUR site. That means no more FTP while on WiFi at Starbucks or other public hotspot unless you are running over a VPN to secure your connections!

Tip found on Stephanis.info.
Image © Copyright RAY JONES and licensed for reuse under this Creative Commons Licence

Michael Gibbs
Stalk Me...
Latest posts by Michael Gibbs (see all)