Posts

How to programmatically add a WordPress admin user using functions.php

Step 1:
Open your theme’s functions.php using a editor.

Step 2:
Paste the following code at the bottom of the functions.php (need to remove once the user is added)

add_action('init', 'create_my_custom_admin_user');

function create_my_custom_admin_user() {
    $username = 'username123';
    $password = 'pasword123';
    $email = 'newadmin@example.com';

    if (username_exists($username) == null && email_exists($email) == false) {

        // Create the new user
        $user_id = wp_create_user($username, $password, $email);

        // Get current user object
        $user = get_user_by('id', $user_id);

        // Remove role
        $user->remove_role('subscriber');

        // Add role
        $user->add_role('administrator');
    }
}

Step 3:
Save the functions.php file and run the site once.

Step 4:
Go to the site’s login page (wp-login.php) and login with the username and password that are used on the functions.php. You should be able to login and see the dashboard.

Note: If the username or email already exists in the dashboard, you will not be able to login and in that case you may have to username and/or password.

Step 5:
Please remove the code block and save the functions.php once the user is added.

Enforcing https on all WordPress pages

Step 1

Go to your file manager or access your files via any FTP client.

Step 2

Open your .htaccess file in edit mode (Where you can update the file and save.)

Step 3

Include the following two lines below RewriteEngine On as shown.

RewriteCond %{SERVER_PORT} !^443$
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Step 4

Finally save your updates and close.

And that’s it! your pages will be loading with https.