Add Admin User in WordPress By FTP

Creating an admin user in WordPress through an FTP client is not a direct method since user accounts are managed through the WordPress database and not through files on the server. To add an admin user, you’ll need to use a different approach. Here’s how you can do it:

1. Access the WordPress Database:
Connect to your website’s hosting account using an FTP client and navigate to the root directory of your WordPress installation. Look for a file called `wp-config.php` and download it to your local computer. This file contains the necessary database configuration details.

2. Edit the `wp-config.php` File:
Open the `wp-config.php` file using a text editor (e.g., Notepad++ or VSCode) and add the following lines just before the line that says `/* That’s all, stop editing! Happy publishing. */`:

define('WP_ADMIN_USERNAME', 'new_admin_username');
define('WP_ADMIN_PASSWORD', 'new_admin_password');
define('WP_ADMIN_EMAIL', 'admin_email@example.com');

Replace `’new_admin_username’`, `’new_admin_password’`, and `’admin_email@example.com’` with the desired username, password, and email for the new admin user.

3. Save the `wp-config.php` File:
Save the changes to the `wp-config.php` file and upload it back to the server, overwriting the existing file.

4. Run the Script:
Now, create a new PHP file in the root directory of your WordPress installation. Name it, for example, `add_admin_user.php`. Add the following code to the new file:

<?php
require_once('wp-config.php');
require_once(ABSPATH . 'wp-includes/wp-db.php');

if (!empty(WP_ADMIN_USERNAME) && !empty(WP_ADMIN_PASSWORD) && !empty(WP_ADMIN_EMAIL)) {
    $user_id = username_exists(WP_ADMIN_USERNAME);

    if (!$user_id) {
        $user_id = wp_create_user(WP_ADMIN_USERNAME, WP_ADMIN_PASSWORD, WP_ADMIN_EMAIL);
        if (!is_wp_error($user_id)) {
            $user = new WP_User($user_id);
            $user->set_role('administrator');
            echo 'Admin user added successfully.';
        } else {
            echo 'Error adding admin user: ' . $user_id->get_error_message();
        }
    } else {
        echo 'Admin user already exists.';
    }
} else {
    echo 'Please define WP_ADMIN_USERNAME, WP_ADMIN_PASSWORD, and WP_ADMIN_EMAIL in wp-config.php.';
}

 

5. Run the Script:
Access the newly created PHP file through your web browser by going to `http://example.com/add_admin_user.php`, where `example.com` is your website’s domain. The script will execute and attempt to create the new admin user based on the defined constants in the `wp-config.php` file.

6. Remove the Script:
Once the admin user has been successfully added, make sure to remove the `add_admin_user.php` file from your server for security reasons.

Please note that using this method to create an admin user should be handled with caution. After running the script, ensure that you delete the `add_admin_user.php` file from the server immediately to prevent unauthorized access. Always keep a backup of your website’s database before making any changes to ensure you can revert to a previous state if needed.

Leave a Comment

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