How to Add an Admin User to WordPress via functions.php

WordPress is a popular content management system used to build websites of all kinds. It provides a user-friendly interface and a wide range of features that can be extended using plugins and themes. However, sometimes you may need to add an admin user to WordPress directly via the functions.php file. In this tutorial, we will show you how to add an admin user to WordPress via functions.php.

Step 1: Access the Functions.php File The first step is to access the functions.php file of your WordPress theme. You can access it by logging in to your WordPress dashboard, navigating to Appearance > Theme Editor, and selecting the functions.php file from the list of files on the right-hand side.

Step 2: Add the Code to the Functions.php File To add an admin user to WordPress via functions.php, you need to add the following code to the file:


function add_admin_user() {
$username = 'newadmin';
$password = 'password123';
$email = '';
if ( !username_exists( $username ) && !email_exists( $email ) ) {
$user_id = wp_create_user( $username, $password, $email );
$user = new WP_User( $user_id );
$user->set_role( 'administrator' );
} } add_action( 'init', 'add_admin_user' );

In this code, you need to replace the values of $username, $password, and $email with the username, password, and email address of the new admin user that you want to add.

Step 3: Save the Changes Once you have added the code to the functions.php file, click on the “Update File” button to save the changes.

Step 4: Verify the New Admin User To verify that the new admin user has been added successfully, log out of your WordPress dashboard and log back in using the username and password of the new admin user that you have just added. You should now have access to the WordPress dashboard with full admin privileges.

Adding an admin user to WordPress via functions.php is a straightforward process that can be done by following the steps outlined in this tutorial. However, it is important to note that you should only add admin users that you trust and to remove any admin users that are no longer needed. This will help to keep your WordPress site secure and prevent unauthorized access.

Leave a Reply

Your email address will not be published. Required fields are marked *