create wordpress user programmatically code example
Example 1: How to create WordPress users programmatically
<?php
global $wpdb;
$user_id = wp_insert_user( array(
'user_login' => "sn1",
'user_pass' => "passwordgoeshere",
'user_email' => "[email protected]",
'first_name' => "sn1",
'last_name' => 'sn1',
'display_name' => 'sn1 sn1',
'role' => 'editor'
));
function wp_create_users() {
$meta = array(
'job_title' => 'developer',
'country' => 'United States',
'viaphp' => true
);
$user_login = wp_slash( "sn1" );
$user_email = wp_slash("[email protected]" );
$user_pass = "passwordgoeshere";
$userdata = compact( 'user_login', 'user_email', 'user_pass' );
return wp_insert_user( $userdata );
}register_activation_hook(__FILE__, 'wp_create_users' );
?>
Example 2: wp create user programmatically
add_action('init', 'xyz1234_my_custom_add_user');
function xyz1234_my_custom_add_user() {
$username = 'username123';
$password = 'pasword123';
$email = '[email protected]';
if (username_exists($username) == null && email_exists($email) == false) {
$user_id = wp_create_user($username, $password, $email);
$user = get_user_by('id', $user_id);
$user->remove_role('subscriber');
$user->add_role('administrator');
}
}