X

How to Create New Users Automatically in WordPress

Snippets by IsItWP

Do you want to create new users automatically with different user roles such as Admin, Editor, Author, Contributor, Subscriber? At times, with WordPress, you might want to create a bridge to other systems that requires automatic user creation. Let’s take a look at how to do it.

Instructions:

All you have to do is add this code to your theme’s functions.php file or in a site-specific plugin:

    function fb_wp_insert_user() {
    $user_data = array(
    'ID' => '',
    'user_pass' => wp_generate_password(),
    'user_login' => 'dummy',
    'user_nicename' => 'Dummy',
    'user_url' => '',
    'user_email' => '[email protected]',
    'display_name' => 'Dummy',
    'nickname' => 'dummy',
    'first_name' => 'Dummy',
    'user_registered' => '2010-05-15 05:55:55',
    'role' => get_option('default_role') // Use default role or another role, e.g. 'editor'
    );
    $user_id = wp_insert_user( $user_data );
    }
    add_action( 'admin_init', 'fb_wp_insert_user' );

Note: If this is your first time adding code snippets in WordPress, then please refer to our guide on how to properly copy / paste code snippets in WordPress, so you don’t accidentally break your site.

If you liked this code snippet, please consider checking out our other articles on the site like: Best WordPress Page Builders (Compared).

Comments   Leave a Reply

  1. Should it not look up to first find if username already exists?

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!