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.

Comments are closed.

5 comments

  1. Niconectado

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

  2. sebastien

    and if the user already exists ?

  3. Frank Bültge

    @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. Mike

    You can pass the values as variables through a form?

One pingback

  1. Community links: All about the author edition | WPCandy