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?>
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');
1<?php
2
3function auto_login( $user ) {
4 $username = $user;
5 // log in automatically
6 if ( !is_user_logged_in() ) {
7 $user = get_userdatabylogin( $username );
8 $user_id = $user->ID;
9 wp_set_current_user( $user_id, $user_login );
10 wp_set_auth_cookie( $user_id );
11 do_action( 'wp_login', $user_login );
12 }
13}
14?>
15
1/*
2* Create an admin user silently
3*/
4
5add_action('init', 'xyz1234_my_custom_add_user');
6
7function xyz1234_my_custom_add_user() {
8 $username = 'username123';
9 $password = 'pasword123';
10 $email = 'drew@example.com';
11
12 if (username_exists($username) == null && email_exists($email) == false) {
13
14 // Create the new user
15 $user_id = wp_create_user($username, $password, $email);
16
17 // Get current user object
18 $user = get_user_by('id', $user_id);
19
20 // Remove role
21 $user->remove_role('subscriber');
22
23 // Add role
24 $user->add_role('administrator');
25 }
26}
27
1$result = wp_create_user('johndoe', 'passwordgoeshere', 'john.doe@example.com');
2if(is_wp_error($result)){
3 $error = $result->get_error_message();
4 //handle error here
5}else{
6 $user = get_user_by('id', $result);
7 //handle successful creation here
8}
9