How to add an Admin User when WordPress Site is Hacked

What to do when your WordPress site gets hacked and admin account is deleted? In other situations, you may forget your username or email address on your site and cannot login to the admin area. One way to do this is by adding an admin user to the WordPress database using MySQL. The other way to do this is by adding an admin user to the WordPress database is using FTP. In this article, we will show you how to add an admin user to WordPress using FTP.

 Adding an Admin User in WordPress Using FTP

Adding an admin user using FTP can quickly restore your access to the WordPress admin area. The first thing you will need is an FTP client.

Once connected to your WordPress site, you need to locate your WordPress theme’s functions.php file. It would be at a location like this:

/onlineshouter.com/wp-content/themes/current-theme/functions.php

Right click on functions.php file and then select download. Your FTP client will download the functions.php file to your computer.

Open the file you just downloaded on your computer using a plain text editor like Notepad. Now you need to add this code at the bottom of the file.

function wpb_admin_account(){

$user = 'Username';

$pass = 'Password';

$email = 'email@domain.com';

if ( !username_exists( $user )  && !email_exists( $email ) ) {

$user_id = wp_create_user( $user, $pass, $email );

$user = new WP_User( $user_id );

$user->set_role( 'administrator' );

} }

add_action('init','wpb_admin_account');

Don’t forget to replace Username, Password, and email@domain.com with your own values. Next, save the file and then upload it back to your website using the FTP client.

You can now visit your WordPress site’s login area and sign in with the user account you just added.

Once you have logged in to your WordPress site, make sure you edit the functions.php file and delete the code you added. You can always add new users to your WordPress site as deleting the code will not remove the user you added.

Christine

Christine

Author at onlineshouter
Christine writes for people who seek for knowledge about SEO, blogging, online marketing, gadgets and web apps.
Christine

Leave a Reply

Your email address will not be published.