Lost access to your WordPress dashboard and need to create an admin user? If you can access your files via FTP, there’s a workaround using the wp-config.php file and a small PHP script. Here’s a secure, step-by-step guide to do that.
Step 1: Connect to Your Server via FTP
Use any FTP client (like FileZilla) to access your WordPress installation. In the root directory, locate and download the wp-config.php file—this contains your site’s database configuration.
Step 2: Define Admin Credentials
Open the wp-config.php file in a code editor. Add the following lines before:
/* That's all, stop editing! Happy publishing. */
//Add:
define('WP_ADMIN_USERNAME', 'your_admin_username');
define('WP_ADMIN_PASSWORD', 'your_admin_password');
define('WP_ADMIN_EMAIL', 'your_email@example.com');
Replace the values with your desired admin username, password, and email.
Step 3: Upload the File
Save and re-upload the modified wp-config.php to the server, overwriting the old one.
Step 4: Create the Script File
Now, create a file named add_admin_user.php in the same directory. Paste this code:
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 created successfully.';
} else {
echo 'Error: ' . $user_id->get_error_message();
}
} else {
echo 'User already exists.';
}
} else {
echo 'Missing user details in wp-config.php.';
}
Step 5: Execute the Script
Go to https://yourdomain.com/add_admin_user.php in your browser. If everything is set up correctly, your new admin user will be created.
Step 6: Remove the Script Immediately
**Important:** For your site’s safety, delete add_admin_user.php right after running it. Leaving it on your server can pose a serious security risk.
Conclusion
This method is a reliable way to recover WordPress admin access using FTP. Make sure to back up your site beforehand and delete the script after use. If unsure, consult a developer.
Useful Resources:
- WordPress: Administrator Role Explained
- WPBeginner: Adding New Users in WordPress
- FTP Overview – WPBeginner
