Home > WordPress > How to Add an Admin User in WordPress Using Functions PHP File?

How to Add an Admin User in WordPress Using Functions PHP File?

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)

To add an admin user in WordPress, you need an FTP (File Transfer Protocol) client-server on your computer. You can use FileZilla OR Cyberduck.

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.

FTP - Connected to the Website
FTP – Connected to the Website

Then move on to your active theme’s functions.php file by visit the following path.

/root/wp-content/themes/your-active-theme/functions.php

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.

FTP - Edit the functions.php file
FTP – Edit the functions.php file


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 init here.

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
    cPanel - Edit File
  • 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.

Photo of author

About Aman Mehra

Hey there! I'm Aman Mehra, a full-stack developer with over six years of hands-on experience in the industry. I've dedicated myself to mastering the ins and outs of PHP, WordPress, ReactJS, NodeJS, and AWS, so you can trust me to handle your web development needs with expertise and finesse. In 2021, I decided to share my knowledge and insights with the world by starting this blog. It's been an incredible journey so far, and I've had the opportunity to learn and grow alongside my readers. Whether you're a seasoned developer or just dipping your toes into the world of web development, I'm here to provide valuable content and solutions to help you succeed. So, stick around, explore the blog, and feel free to reach out if you have any questions or suggestions. Together, let's navigate the exciting world of web development!

Leave a Comment