A WordPress powered website on the internet is a sophisticated and complex system. This complex system is built and supported by many different types of software, sub-systems, etc. As a result, all it takes for a platform like WordPress and a website powered by it to stop working is a failure of one of these support systems.  

Therefore a WordPress developer must be prepared for all eventualities. A WordPress website is generally protected behind a wall of security encryption. This security encryption stops unwanted intrusion into the control & admin console of the website. But a door created to keep intruders out can also lock you out. 

If your website is attacked and your admin is deleted, there is very little you can do to save your site. The security encryption made to protect you and your interests can turn into your enemy real quick. Thus, it is a must that you know how to circumvent such an unsavory situation.   

How To Add A New Admin User Via PHP?

The only way you can access your website in such a scenario is by creating a new admin login. Now the question comes, how do you create a new admin to your WordPress website? There are two ways of accessing your database and adding a new admin. You can either go with the MySQL route or the PHP route. On the MySQL route, it will take you longer to complete the task, therefore we will look at the easier PHP route.  

A majority of WordPress developers are familiar with PHP that makes it simpler to understand the following hack. The following steps detail how you can add a new admin to your WordPress database through PHP:

Step – 1: Add a new mu-plugins/ directory to your WordPress website’s wp-content/ directory

Step – 2: Create and add a new file to the wp-content/mu-plugins/ directory. 

Step – 3: Add the following code snippet into the file you just created inside your wp-content/mu-plugins/ directory: 

 

<?php

add_action( 'init', function () {

$username = 'admin';

$password = 'password';

$email_address = '
        
            we*******@my******.com
            
                
                
                
            
            
                
                
                
            
        
';

if ( ! username_exists( $username ) ) {

$user_id = wp_create_user( $username, $password, $email_address );

$user = new WP_User( $user_id );

$user->set_role( 'administrator' );

}

} );


Step – 4: Now add your username, password, and email

Step – 5: Write your new admin login details to your website login console and enter your website.

Step – 6: After you successfully enter your website, make sure to delete the code snippet file.

Share this article