Last active
August 7, 2023 16:31
-
-
Save benheu/4a299d20a97aa8ec96ffce145e77fa71 to your computer and use it in GitHub Desktop.
Create a WP user when a new booking is made: Shared by a Wappointment user (Gil)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_action('wappointment_appointment_confirmed', 'my_function'); | |
function my_function($eventObject){ | |
$client = $eventObject->getClient(); | |
$email = $client->email; | |
$name = $client->name; | |
// Check if the user already exists | |
$user = get_user_by('email', $email); | |
// If user doesn't exist, create a new one | |
if (!$user) { | |
// Generate a random password | |
$password = wp_generate_password(); | |
// Create the user | |
$user_id = wp_create_user($email, $password, $email); | |
// Set user first name | |
wp_update_user(array('ID' => $user_id, 'first_name' => $name)); | |
// Set user display name as first name + last name | |
$user_data = array( | |
'ID' => $user_id, | |
'display_name' => $name, | |
'user_nicename' => sanitize_title($name) | |
); | |
wp_update_user($user_data); | |
// Send a welcome email to the user | |
wp_new_user_notification($user_id, null, 'both'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment