Dani 1 min read
January 15, 2024 (last revised)

How To Create a WordPress User Programmatically

Managing user accounts efficiently is crucial for a seamless user experience on your WordPress site. In this guide, we’ll walk you through the process of programmatically adding new users to your WordPress site. The beauty of this approach is that it can be seamlessly integrated into your child theme’s functions.php file, offering a streamlined solution. Let’s dive in!

Step 1: Gather WordPress User Information

// Gather user information
$username = 'new_username';
$email = 'user@example.com';
$password = 'user_password';  // Replace with a secure password

This step involves setting up the basic information for the new user, including the desired username, email address, and password. Make sure to replace the placeholder values with actual user data.

Step 2: Check User Existence

// Check if the username or email already exists
$user_id = username_exists($username);

if (!$user_id && email_exists($email) == false) {
    // Proceed to create a new user
} else {
    // Username or email already exists
}

Here, the code checks whether the specified username ($username) exists. If not, it further checks if the provided email address ($email) is already associated with an existing user. If both conditions are false (indicating that the username and email are not already in use), the script proceeds to create a new user. Otherwise, it indicates that the username or email already exists.

Step 3: Create a New WordPress User

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

if (is_wp_error($user_id)) {
    // Handle error if user creation fails
    echo 'Error creating user: ' . $user_id->get_error_message();
} else {
    // User created successfully
    echo 'User created with ID ' . $user_id;
}

If the conditions in Step 2 are met, this part creates a new user using the wp_create_user function. It checks if the user creation process returns a WordPress error. If an error occurs, it handles the error and outputs an error message.

Step 4: Implement User Creation Code

Copy and paste the following complete code snippet into your child theme’s functions.php file:

// Step 1: Gather User Information
$username = 'new_username';
$email = 'user@example.com';
$password = 'user_password';  // Replace with a secure password

// Step 2: Check User Existence
$user_id = username_exists($username);

if (!$user_id && email_exists($email) == false) {
    // Step 3: Create a New User
    $user_id = wp_create_user($username, $password, $email);

    if (is_wp_error($user_id)) {
        // Handle error if user creation fails
        echo 'Error creating user: ' . $user_id->get_error_message();
    } else {
        // User created successfully
        echo 'User created with ID ' . $user_id;
    }
} else {
    // Username or email already exists
    echo 'Username or email already exists';
}

 

New user created

Conclusion

By incorporating these steps into your child theme’s functions.php file, you can effortlessly enhance your WordPress site with the ability to programmatically create new users. This approach is not only powerful but also provides a user-friendly solution. Test thoroughly in a controlled environment before deploying changes to your live site, and enjoy the streamlined user management on your WordPress website. Happy coding!

PHP Wordpress