How to Add an Admin User in WordPress via FTP
Adding a new admin user in WordPress through an FTP client is not a straightforward process because WordPress user accounts are managed within the database. However, there is a way to achieve this by modifying the wp-config.php
file and running a custom script. Below is a step-by-step guide to adding an admin user through FTP.
1. Access the WordPress Database
To begin, connect to your website’s hosting account via an FTP client and navigate to the root directory of your WordPress installation. Locate the wp-config.php
file and download it to your local computer. This file contains the essential database configuration details for your WordPress site.
2. Edit the wp-config.php
File
Open the wp-config.php
file using a text editor (e.g., Notepad++ or VSCode). Add the following lines just before the line that reads /* 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');
Be sure to replace 'new_admin_username'
, 'new_admin_password'
, and 'admin_email@example.com'
with your desired username, password, and email address for the new admin user.
3. Save and Upload the File
Save the wp-config.php
file and upload it back to the server, overwriting the existing file. This will apply the changes to your WordPress installation.
4. Create the Admin User Script
Next, create a new PHP file in the root directory of your WordPress installation. Name it add_admin_user.php
and add the following code to this 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
To run the script, open your browser and navigate to http://yourdomain.com/add_admin_user.php
, replacing yourdomain.com
with your actual website domain. The script will execute and attempt to create the new admin user based on the parameters defined in your wp-config.php
file.
6. Remove the Script for Security
Once the admin user has been successfully created, it’s essential to delete the add_admin_user.php
file from your server for security reasons. Leaving the script on your server can expose your site to potential security threats.
Important: Be cautious when using this method. Always back up your website’s database before making any changes to ensure you can revert to a previous state if needed.
Conclusion
Using FTP to create an admin user in WordPress is a useful method when you have lost access to the WordPress dashboard. Just remember to follow the steps carefully and prioritize security. If you’re not comfortable with modifying files manually, consider seeking professional help.