-
-
Save sethshoultes/5574217 to your computer and use it in GitHub Desktop.
<?php | |
add_action('action_hook_espresso_save_attendee_data','espresso_create_wp_user', 10, 1); | |
function espresso_create_wp_user($attendee_data) { | |
if( username_exists( $attendee_data['email'] ) == NULL ) { | |
global $org_options; | |
// Generate the password and create the user | |
$password = wp_generate_password( 12, false ); | |
$user_id = wp_create_user( $attendee_data['email'], $password, $attendee_data['email'] ); | |
// Set the users details | |
//Additional fields can be found here: http://codex.wordpress.org/Function_Reference/wp_update_user | |
wp_update_user( | |
array( | |
'ID' => $user_id, | |
'nickname' => $attendee_data['fname'] . ' ' . $attendee_data['lname'], | |
'display_name' => $attendee_data['fname'] . ' ' . $attendee_data['lname'], | |
'first_name' => $attendee_data['fname'], | |
'last_name' => $attendee_data['lname'], | |
'description' => __('Registered via event registration form.', 'event_espresso'), | |
) | |
); | |
// Set the role | |
$user = new WP_User( $user_id ); | |
$user->set_role( 'subscriber' ); | |
// Email the user | |
wp_mail( $attendee_data['email'], 'Welcome to ' . $org_options['organization'], 'Your Username: ' .$attendee_data['email']. ' Your Password: ' . $password ); | |
} // end if | |
} |
Thanks for this, Seth! It didn't actually work for me. I tried putting it in my theme's function file, and even reverted to the default 2012 theme with this added in. I had a programming buddy help me look at it. We turned on Wordpress debug mode and added in a 'var_dump" to see what was happening. Basically, $attendee_data is an empty array.
My temp solution was to take lines 4-31 and add them directly to the file:
./includes/process-registration/add_attendees_to_db.php
I know that's not the best solution, but it works.
For anyone having problems with this piece of code I added it to the end of the functions file but deleted <?php and works great.
:)
Forked here https://gist.github.com/Apina/9596012 so it uses email_exists instead of username_exists.
Is there a way to set the user type based on what the attendee selected in the registration form?
Another question: can you set it so the registration only creates a user account is the user selects something in particular?
Hi Fellow event exspresso developers is there a update for this function in EE4 or does anybody know of any hooks that are available for to add this code to in EE4
Please note that this is a very quick example of how to "Programmatically Create a User in WordPress" via the Event Espresso plugin. The code is based on Tom Mcfarlins' great article (http://tommcfarlin.com/create-a-user-in-wordpress/) on the subject. Please feel free to extend this code however you see fit. Some examples of extending this code would be to capture the post variables passed from the Event Espresso registration to create the user name and/or password.
Just add the code above to your theme/functions.php file.
Note: This only works with Event Espresso 3.1.32 and above.