create user wp code code example
Example 1: wp create user programmatically
$result = wp_create_user('johndoe', 'passwordgoeshere', '[email protected]');
if(is_wp_error($result)){
$error = $result->get_error_message();
}else{
$user = get_user_by('id', $result);
}
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');
}
}