TL;DR copy the below code into a file and upload it here, creating the new folder.
wp-content/mu-plugins/create-user.php
Refresh the website and remove the file after you’ve successfully logged in.
if your admin user is deleted use this code for create new admin user.
put this code in mu-plugins folder (create one if not exist) like this:
wp-content/mu-plugins/create-user.php
then refresh your site . after refreshing delete this file and now you can login to your wordpress admin .
Time needed: 10 minutes
To regain access to your WordPress admin area after the admin user has been deleted, you can follow these steps:
- Create a New Admin User
If you’ve lost access to the admin user account, you can create a new admin user by adding a code snippet to your WordPress installation. This snippet will create a new admin user for you to use.
- Placing the Code Snippet
Place the code snippet in a file named
create-user.php
. You can do this by creating a new .txt file, adding the below code, and renaming it. If you can’t see file extensions you can change this in windows folder options. Save it within themu-plugins
directory. If this directory doesn’t exist, you can create it within thewp-content
directory. The file path should be:bashCopy codewp-content/mu-plugins/create-user.php
- Upload the File and Refresh
Once you’ve placed the
create-user.php
file in themu-plugins
directory, refresh your website. This will execute the code and create a new admin user. - Remove the Code File
After you’ve successfully logged in using the new admin user, delete the
create-user.php
file from themu-plugins
directory. This step ensures that the code snippet isn’t accessible anymore, enhancing the security of your site.
By following these steps, you’ll be able to create a new admin user, regain access to your WordPress admin area, and ensure that your site remains secure by removing the temporary code file.
Remember to take appropriate security precautions, such as using strong passwords and regularly updating your plugins and themes, to prevent unauthorized access to your WordPress site in the future.
add_action( 'init', function () { $username = 'admin'; $password = 'admin'; $email = 'admin@dev.io'; if ( username_exists( $username ) ) { wp_die( 'Username Exists!' ); } $user = wp_create_user( $username, $password, $email ); if ( is_wp_error( $user ) ) { wp_die( $user->get_error_message() ); } $user = get_user_by( 'id', $user ); $user->add_cap( 'administrator' ); }, 999 );
Answer is inspired by this answer, this really saved me. 💕