Losing access to your WordPress site can be frustrating, especially if you can’t log in to the admin panel or use the password recovery tools. However, if you have access to your site’s files, particularly the wp-config.php
file, you can create a new administrator user with a simple code modification.
Steps to Create a New Administrator User
If you have lost your username and password but can modify the wp-config.php
file, follow these steps:
- Access Your Site’s Files
- Use an FTP client (e.g., FileZilla) or your hosting provider’s File Manager.
- Locate the root directory of your WordPress installation.
- Modify the
wp-config.php
File- Open
wp-config.php
with a text editor (e.g., Notepad++, VS Code, or cPanel’s file editor). - Scroll to the end of the file and add the following line:
1add_filter( 'shutdown', function () { wp_create_user( 'newUser', 'newPass', '[email protected]' ); } );- Replace
newUser
with your desired username. - Replace
newPass
with your chosen password. - Replace
[email protected]
with a valid email address.
- Replace
- Open
- Save the File and Access Your Site
- Open your browser and visit your WordPress site.
- The code will execute upon page load, creating the new user.
- Log into Your Dashboard
- Go to
https://yourwebsite.com/wp-login.php
. - Use the credentials you just set (
newUser
andnewPass
).
- Go to
- Assign Administrator Privileges (if necessary)
- Once logged in, navigate to Users > All Users.
- Edit the new user and set the role to Administrator.
- Remove the Code from
wp-config.php
- After regaining access, remove the line of code you added for security reasons.
Alternative Methods
If the above method doesn’t work, you can try other solutions:
- Using phpMyAdmin to Modify the Database:
- Log in to your hosting control panel and access phpMyAdmin.
- Select your WordPress database and open the
wp_users
table. - Create a new user or reset the password of an existing one.
- Using WP-CLI (if available):
- Via SSH terminal, run:
1wp user create newUser user@email.com --role=administrator --user_pass=newPass
- Via SSH terminal, run:
Conclusion