WordPress-Security

How to Create WordPress Administrative Account via FTP/File Manager

Sometimes we got ourselves locked out of WordPress backend, mostly because of forgetting password. In most cases your could reset password via Email. But you might sometimes forget your email password too. Oh, No! that’s terrible.

Here I introduce a method to create a new temporary WordPress administrative account via FTP/File Manager, so you could get in, change your old account’s password, then login in with your old account, then delete the temporary account.

The code snippet used below is not my original work, I don’t remember where I got it…

<?php
// ADD NEW ADMIN USER TO WORDPRESS
// ----------------------------------
// Put this file in your WordPress root directory and run it from your browser.
// Delete it when you're done.

require_once('wp-blog-header.php');
require_once('wp-includes/registration.php');

// CONFIG
$newusername = 'Test';
$newpassword = 'Test';
$newemail = '[email protected]';

// Make sure you set CONFIG variables
if ( $newpassword != 'YOURPASSWORD' && $newemail != [email protected]' && $newusername !='YOURUSERNAME'  ) 
{
    // Check that user doesn't already exist
    if ( !username_exists($newusername) && !email_exists($newemail) ) 
    {
        // Create user and set role to administrator
        $user_id = wp_create_user( $newusername, $newpassword, $newemail);
        if ( is_int($user_id) )
        {
            $wp_user_object = new WP_User($user_id);
            $wp_user_object->set_role('administrator');
            echo 'Successfully created new admin user. Now delete this file!';
        } 
        else {
            echo 'Error with wp_insert_user. No users were created.';
        }
    } 
    else {
        echo 'This user or email already exists. Nothing was done.';
    }
} 
else {
    echo "Whoops, looks like you didn't set a password, username, or email before running the script. Set these variables and try again.";
}

Create a new php file and put in the code. Change the bolded words to your needs. that would be your temporary account’s credentials.

$newusername = 'Test';
$newpassword = 'Test';
$newemail = '[email protected]';

Upload the php file to the root directory of your WordPress. and visit the file in your browser. for example:

http://www.website.com/create_new_account.php

Once visited, the new account with credentials your set just now shall be created.

Leave a Reply

Your email address will not be published. Required fields are marked *