Locked out of your WordPress dashboard? Don’t worry—you can still create a new admin account by accessing your site files through FTP. This guide walks you through the safe and efficient way to do it.
Step 1: Open FTP and Locate wp-config.php
Using your FTP client, navigate to your WordPress site’s root directory. Download the wp-config.php file—it holds essential configuration, including database access.
Step 2: Insert Admin Details
Open the wp-config.php in a text editor and add the following just before this line:
/* 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');
Update these with your preferred username, password, and email.
Step 3: Save and Upload Back
Upload the modified wp-config.php back to the server, overwriting the existing version.
Step 4: Add PHP Script to Register Admin
Create a new file in the root directory called add_admin_user.php and paste the code below:
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
Visit https://yourdomain.com/add_admin_user.php in a browser. If set up correctly, this will create a new admin user.
Step 6: Delete the Script
For your website’s safety, delete add_admin_user.php immediately after use. Leaving it accessible is a major security risk.
Final Thoughts
This FTP-based method is great for recovering access or resolving user issues. Just remember to delete the script after execution and always back up your site before making changes.
Helpful Links:
