Create Users Automatically In WordPress

WordPress is a web application by many – often you have to create a bridge to other systems. For example, to give users a single sign-on via the web applications. Therefore, automated creating users might be necessary. Depending on which system is the leader, the data must be synchronized. From past experience, the users are usually created in WordPress, as this is not the leading system for user administration. WordPress has the function wp_insert_user() for the automated creation of users.

The function just needs some parameters and you are good to go. In the following example I created the code so that it creates a user as soon as you go to the backend. For this you can use the functions.php of your Theme or a Plugin, after that you can take out the code.

For example, to create a bridge, there belongs much more to the code – this should only serve the function as an example and idea. You can find more information in the documentation of the function or in the core.

function fb_wp_insert_user() {
	$user_data = array(
		'ID' => '',
		'user_pass' => wp_generate_password(),
		'user_login' => 'dummy',
		'user_nicename' => 'Dummy',
		'user_url' => '',
		'user_email' => 'dummy@example.com',
		'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' );

To keep the Bridge maintain and up to date and users synchronized, there is a function for updating a user – wp_update_user.

Similar scenarios are also possible for creating articles, therefore is also a function available, which we explained in another post.


Posted

in

by

Tags:

Comments

5 responses to “Create Users Automatically In WordPress”

  1. Niconectado Avatar
    Niconectado

    Great function! Now i look forward to make it work with bulk data from a .csv file or sql DB . Thanks!

  2. sebastien Avatar
    sebastien

    and if the user already exists ?

  3. Frank Bültge Avatar

    @sebastien: the function check this, you must not inlcude this iside your function; but it is easy to include an if exist for the user-id. This code is only an example for use it.

  4. […] Bültge posted a code snippet to WPEngineer that will automatically create users within a WordPress […]

  5. Mike Avatar
    Mike

    You can pass the values as variables through a form?