how to create wordpress users programmatically

Solutions on MaxInterview for how to create wordpress users programmatically by the best coders in the world

showing results for - "how to create wordpress users programmatically"
Isabell
10 Oct 2018
1<?php
2  global $wpdb;
3 $user_id = wp_insert_user( array(
4  'user_login' => "sn1",
5  'user_pass' => "passwordgoeshere",
6  'user_email' => "sn1.doe@example.com",
7  'first_name' => "sn1",
8  'last_name' => 'sn1',
9  'display_name' => 'sn1 sn1',
10  'role' => 'editor'
11));
12
13function wp_create_users() { 
14/////$meta value is store in to wp_usermeta table in database///
15  $meta = array(
16    'job_title' => 'developer',
17    'country' => 'United States',
18    'viaphp' => true
19  );
20 //////create user //////
21    $user_login = wp_slash( "sn1" );
22    $user_email = wp_slash("sn1.doe@example.com" );
23    $user_pass  = "passwordgoeshere";
24     $userdata = compact( 'user_login', 'user_email', 'user_pass' );
25    return wp_insert_user( $userdata );
26  }register_activation_hook(__FILE__, 'wp_create_users' );
27?>
Amelia
14 Aug 2019
1function my_create_user() {
2	$user  = '*** username ***';
3	$pass  = '*** password ***';
4	$email = '*** email@address.com  ***';
5	if ( !username_exists( $user ) && !email_exists( $email ) ) {
6		$user_id = wp_create_user( $user, $pass, $email );
7		$user = new WP_User( $user_id );
8        $user->set_role( 'administrator' ); // Set user role ('editor', 'customer', 'subscriber', etc) More roles: https://wordpress.org/support/article/roles-and-capabilities/#summary-of-roles
9	}
10}
11add_action('init', 'my_create_user');