How to Add a WordPress Administrator User Using PHP Code

How to Add a WordPress Administrator User Using PHP Code

Once in a while, you may in need to add an administrator user to your WordPress website. Nowadays, bloggers are opening up their doors to guest authors.

And it’s evident that it’s not hard to manage a website alone, you may also need a helping hand.

Generally, you may not need to deal with codes while adding a new user, but when you lose your login details or your login limit plugin blocks you, you require to create a new administrator account, it’s possible only using cPanel.

You can use phpMyAdmin, which require an SQL query and I am sure, you may not be familiar with the database coding.

It’s better to use a function and remove it after the work is done. Isn’t this amazing to add a WordPress administrator user using PHP and remove it?

Most of the time, you need to copy, paste the code, and leave it in the file, but not in this method. You’re going to learn the basics of WordPress functions and where to add them.

Do You Know How to Edit the functions.php File

If you’re a techie person, you may already know, the functions of a WordPress website reside in the functions.php file.

Let me make it simple for you. In every WordPress theme, functions.php is the main file to control all the functionalities of the layout; you can use it to enhance your current website features.

You can edit the file from your wp-admin panel, but when you don’t have access to it, you need to use cPanel. I am going to walk you through the process.

Follow the steps.

Step 1:

Open the cPanel account and search for the file manager. You can easily find it under the Files’ section.

How to Add a WordPress Administrator User Using PHP Code

Depending on the web hosting company you use, you can find the file manager. For Bluehost users, cPanel is blue, Siteground has the similar layout as the screenshot, Inmotionhosting provides the same.

The layout may vary, but the options are similar.

Step 2:

If you have only one domain name, it’s evident that the data is residing in the root directory. It’s possible that you may see the home directory, click on public_html from the left-hand sidebar.

How to Add a WordPress Administrator User Using PHP Code

Now, you can see all the files and folders of your WordPress website. Search for the wp-content folder and open it.

Step 3:

On this page, you can see, the folder consists of plugins, themes, and the media data of your site.

How to Add a WordPress Administrator User Using PHP Code

As you already know, you need to open the themes folder.

Step 4:

Here you can see all activated and deactivated themes you have installed on your site. Open your active theme folder.

How to Add a WordPress Administrator User Using PHP Code

Step 5:

Search for the functions.php file and right-click to edit. You can also use the common Edit link showing on the main horizontal navigation menu of cPanel.

How to Add a WordPress Administrator User Using PHP Code

Step 6:

In the code editor, you need to add the code at the bottom.

function new_admin_account(){
$user = 'Username';
$pass = 'Password';
$email = 'email@yoursite.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','new_admin_account');

Click on the Save Changes button.

Note: Don’t forget to change the “Username” and “Password” from the code. You should also change the email address.

Let me show you an example. If I choose “onetwo55” as a username and “in##*os67” as password, the code becomes like this.

function new_admin_account(){
$user = 'onetwo55';
$pass = 'in##*os67';
$email = 'email@yoursite.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','new_admin_account');

Now, trying logging in your new account with these login credentials, if you can log in, congrats, you have successfully created an administrator account for your WordPress site.

Note: Now, you can remove the code you have added.

The beauty of using a function is that you don’t need to keep the code forever, once you finish the task, remove the code and enjoy.

I Hope It’s Not So Hard to Understand

To accomplish one task, you have many alternatives. Many people rely on the WordPress admin panel, but they cry when the website goes down or they lose the login credentials.

You may also think about recovering the password, but sometimes, it can’t work because the security plugin blocks the access.

Creating a new user account is the only hope, which is possible only if you know how to use cPanel. PHP can be scary to many, but all you need is to copy, paste, and remove the code.

Conclusion

It’s essential to learn the basics of cPanel. For a WordPress website, cPanel is like the ultimate weapon. Although many websites are running on managed web hostings, NGINX server, but not all of them.

Most of the small business owners rely on shared web hostings, and cPanel is what you get. I hope you can easily create a new administrator user account.

Check out these top 3 WordPress hosting services:>

A2 Hosting
$1.95 /mo
Starting price
Visit A2 Hosting
Rating based on expert review
  • User Friendly
    4.5
  • Support
    4.0
  • Features
    4.5
  • Reliability
    4.8
  • Pricing
    4.0
IONOS
$1.00 /mo
Starting price
Visit IONOS
Rating based on expert review
  • User Friendly
    4.5
  • Support
    4.0
  • Features
    4.5
  • Reliability
    4.5
  • Pricing
    4.3
Webdock
$1.05 /mo
Starting price
Visit Webdock
Rating based on expert review
  • User Friendly
    3.8
  • Support
    4.5
  • Features
    4.5
  • Reliability
    4.3
  • Pricing
    4.3

How to Disable PHP Execution to Improve Website's Security Using cPanel

This how-to guide will explain about restricting others from executing any malic
less than a minute
Max Ostryzhko
Max Ostryzhko
Senior Web Developer, HostAdvice CTO

How to Add Google Fonts to Your WordPress Website Without a Plugin

This is a how-to guide consisting of the code editing of the WordPress website t
less than a minute
Bruno Mirchevski
Bruno Mirchevski
Hosting Expert

How to Add Custom Code to Header and Footer Areas of a WordPress Website

The tutorial will have two methods of adding the custom code to the header and f
less than a minute
Bruno Mirchevski
Bruno Mirchevski
Hosting Expert

How to Fix an HTTP Error Appearing in the WordPress Media Library

This is an article which will help you solve the HTTP error you get in the media
less than a minute
Eliran Ouzan
Eliran Ouzan
Web Designer & Hosting Expert
HostAdvice.com provides professional web hosting reviews fully independent of any other entity. Our reviews are unbiased, honest, and apply the same evaluation standards to all those reviewed. While monetary compensation is received from a few of the companies listed on this site, compensation of services and products have no influence on the direction or conclusions of our reviews. Nor does the compensation influence our rankings for certain host companies. This compensation covers account purchasing costs, testing costs and royalties paid to reviewers.
Click to go to the top of the page
Go To Top