Being locked of your WordPress admin is frustrating. That’s why we have shown you how to add an admin user in WordPress via MySQL. Recently one of our readers asked if it was possible to add an admin user in WordPress using FTP. Ofcourse it is. In this article, we will show you how to add an admin user in WordPress using FTP.
Why You May Need to Add an Admin User in WordPress Using FTP?
Sometimes you may forget your username or email address on a WordPress 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. But you may be unable to connect to phpMyAdmin or don’t want to run MySQL queries directly.
Some users may get their site hacked and admin account deleted. In that case, adding an admin user using FTP can quickly restore your access to the WordPress admin area.
Having said that, let’s see how you can easily add an admin user in WordPress using FTP access.
Adding an Admin User in WordPress Using FTP
First thing you will need is an FTP client. If you are new to using FTP, then see our guide on how to use FTP to upload files in WordPress.
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:
/yoursite.com/wp-content/themes/your-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 protected]'; 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 protected] 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, please edit the functions.php file and delete the code you added. Deleting the code will not remove the user you added, and you can always add new users and authors to your WordPress site.
We hope this article helped you learn how to add an admin user to WordPress using FTP. You may also want to see our list of 13 vital tips and hacks to protect your WordPress admin area.
If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.
Faisal says
Worked like a charm.
Thank you
WPBeginner Support says
You’re welcome!
Admin
Piotr Szcześniak says
Never remeber the snippet so I’m happy to frequently visit you guys! Thx!
Thomas Hansen says
If you are using a child theme is it then the functions.php of the child theme, that you need to insert the code into?
WPBeginner Support says
Yes, you would want to add the code there.
Admin
André Cassiano says
Worked like a charm! Tks.
WPBeginner Support says
Glad our guide was able to help
Admin
imran says
worked, Thanks!!
WPBeginner Support says
You’re welcome
Admin
Kim H says
Worked like a charm. Thank you for this article!
WPBeginner Support says
You’re welcome
Admin
Donovan says
Worked perfectly. Thanks for the great advice.
WPBeginner Support says
Glad our article was helpful
Admin
Peter says
Thanks! It worked!
WPBeginner Support says
You’re welcome, glad our guide was helpful
Admin
Rogério says
Nice work
WPBeginner Support says
Thanks
Admin
adam says
Thanks man it worked even in WordPress version 4.9.0
Many Thanks brother !
Warner Mendenhall says
Thank you. Worked perfectly.
charu says
Is any way to delete that entry from database that I have created through function.php i.e delete the last created user for using admin area.
Thanks
Shafaq says
good explanation but there is an error with my code. user role changed if go into user profile page otherwise no effect . can you please elaborate.
here is my code
$level = $profileuser->ihc_user_levels ;
$id = $profileuser->id ;
$u= new WP_User( $id ) ;
if ( $level==’3′) {
echo ‘user level ‘.$level;
echo ‘user id ‘.$id;
$u = wp_update_user( $u->set_role( ‘featured’ ) );
}
Stef says
I got the big problem that this new user has first to be activated..
Is it possible to set the activation of the user by function or did I lose and I need to install a backup on my wordpress via ftp?
Rdas says
I have latest WordPress version will it work for me or need to change anything on the code?
Guille says
Great explanation. Thank you so much for this tip. It works like a charm!
Hardeep says
Hello,
Really informative post, I am developer and sometimes I have to contact sys admin to reset or create new wp user for testing.
This is very good way, thanks for sharing
Toni Weidman says
How secure is this going to be on an FTP host?
Patrick Johanneson says
Not very. So the first thing you should do once you’re logged back in is change the password on that new admin account. Then remove the code from functions.php.
Andre says
Start with SFTP with an FTP client (program) that has that. But as Patrick mentioned, you NEED to change your password once you are back in. I don’t mean a simple password either, always make sure you use at least 12 characters mixed with numbers, symbols, lower and uppercase letters (avoid using words).