Admin users of WordPress have full control of the website and all the capabilities and can access everything in the admin area. So in case, you want to add an admin user in WordPress with a code, then you have to write the code in the functions.php file.
So, in this article, we will learn how to add an admin user in WordPress using the functions.php file.
You can do this with FTP (FileZilla) OR through cPanel directly.
Let’s start with it.
Why You Want to Add an Admin User in WordPress Using Code (Functions.php file)
There can be many cases like:
- Forget username and password
- Your site is hacked
- Blocked login attempt
When you forget the username/email address and password, and if your site hacked and you are not able to login to the admin dashboard, then you may need to add a new admin user using code in the functions.php file.
You can also use phpMyAdmin to add an admin user to WordPress using SQL query with have database access. But if you may not familiar with the database and afraid to run SQL queries directly in the database.
Then it’s better to use the functions.php file to add an admin user in WordPress. You have to add a simple function that is super easy and once it is done then you have to remove that function from the file.
Let’s jump on the main point of this article.
Add an Admin User in WordPress Using functions.php file (With FTP)
OK. Let’s connect to the website through FTP credentials. When you are connected and able to see your website files. You will see the screen below.
Then move on to your active theme’s functions.php file by visit the following path.
I’ll suggest you use a child theme to add the custom code. Here are step by step guide on how to create a WordPress child theme
And here you will see the functions.php file. To edit this functions.php file with right-click on it and then click the View/Edit option. It will open the file in the editor and you can edit it now.
When the file opened in the editor, you have to add a custom function code at bottom of the file. So add the below code in your theme’s functions.php file and save it and upload it through FTP.
NOTE: Remember to replcae $username, $password, and $email_address variable’s values with your own values.
The above code will execute when finished the loading of your website from the frontend.
We used the
init action hook for this. You can read more about
Now visit the WordPress admin login page and sign with the username and password that you have just created. You will see, you can log in to the admin dashboard area now.
OK. Now open your functions.php file again and remove that function from the file.
NOTE: Don’t worry. It will not remove the user from the WordPress that you have just added.
Add an Admin User in WordPress (With cPanel)
For this method, you must have cPanel access to edit the file of your website.
To add an admin user in WordPress using cPanel, follow the below steps:
- Login to your cPanel account
- Open the active theme folder
- You will see the functions.php here
- Right click on the functions.php file and click on the Edit option
- It will open the file in edit mode and you can add the above function in this file at bottom
- Save it and test with your username and password
- Once it done, remove that function from file and save it
- That’s it
Hope you understand this article about how to add an admin user in WordPress using functions php file. If you still have any questions, you can ask me in the comment section, I’ll help you with that.