Create new user automatically via functions.php

Created by WP Fix It, Modified on Wed, 6 Mar, 2024 at 4:03 AM by WP Fix It

add_action('init', 'add_my_user');
function add_my_user() {
    $username = 'username_here';
    $email = 'email_here';
    $password = 'password_here';

    $user_id = username_exists( $username );
    if ( !$user_id && email_exists($email) == false ) {
        $user_id = wp_create_user( $username, $password, $email );
        if( !is_wp_error($user_id) ) {
            $user = get_user_by( 'id', $user_id );
            $user->set_role( 'administrator' );
        }
    }
}
PHP

Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select at least one of the reasons
CAPTCHA verification is required.

Feedback sent

We appreciate your effort and will try to fix the article