Skip to content

Instantly share code, notes, and snippets.

@Apina
Forked from sethshoultes/espresso_create_wp_user.php
Last active August 29, 2015 13:57
Show Gist options
  • Save Apina/9596012 to your computer and use it in GitHub Desktop.
Save Apina/9596012 to your computer and use it in GitHub Desktop.
Swapped this to check for the email rather than username, and to dampen any errors due to duplicate username. The code can be added to the themes functions.php file though it is recommended to use a site specific plugin: http://eventespresso.com/wiki/create-site-specific-plugin-wordpress-site/
add_action('action_hook_espresso_save_attendee_data','espresso_create_wp_user', 10,1 );
function espresso_create_wp_user($attendee_data) {
if( email_exists( $attendee_data['email'] ) == false ) {
global $org_options;
// Generate the password and create the user
$password = wp_generate_password( 12, false );
$user_id = wp_create_user( $attendee_data['fname'] . $attendee_data['lname'], $password, $attendee_data['email'] );
if( is_wp_error( $user_id ) ) { return false; }
// 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['fname'] . $attendee_data['lname'] . ' Your Password: ' . $password );
//this will send the user the STANDARD user registration email containing their password - uncomment the line below and comment out the above line if you wish to use this.
//wp_new_user_notification( $user_id, $password);
} // end if
//attach this event to the user.
event_espresso_add_user_to_event($attendee_data['event_id'], $user_id, $attendee_data['attendee_id']);
}
/****************************************************************************/
The code below is an alternate version which uses a question to allow the attendee to set a password.
It does not check the complexity of the password.
TEXT_11 should be changed to be the ID of the question field you wish to use
(you will need to use Chrome Inspector or similar to find this)
/****************************************************************************/
add_action('action_hook_espresso_save_attendee_data','espresso_create_wp_user', 10,1 );
function espresso_create_wp_user($attendee_data) {
// var_dump($attendee_data);
// var_dump($_POST);
// wp_die();
if( email_exists( $attendee_data['email'] ) == false ) {
global $org_options;
// Generate the password and create the user
if( isset($_POST['TEXT_11'])) {
$password = $_POST['TEXT_11'];
}
else {
$password = wp_generate_password( 12, false );
}
$user_id = wp_create_user( $attendee_data['fname'] . $attendee_data['lname'], $password, $attendee_data['email'] );
if( is_wp_error( $user_id ) ) { return false; }
// 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['fname'] . $attendee_data['lname'] . ' Your Password: ' . $password );
//this will send the user the STANDARD user registration email containing their password - uncomment the line below and comment out the above line if you wish to use this.
//wp_new_user_notification( $user_id, $password);
} // end if
//attach this event to the user.
event_espresso_add_user_to_event($attendee_data['event_id'], $user_id, $attendee_data['attendee_id']);
}
@earthjibber
Copy link

Will this work for EE4?

@Apina
Copy link
Author

Apina commented Jul 29, 2014

Hi @earthjibber. I didn't see this until now so my apologies, but no it will not work for EE4.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment