Skip to content

Instantly share code, notes, and snippets.

@joshfeck
Forked from Pebblo/espresso_create_wp_user.php
Last active August 29, 2015 14:08
Show Gist options
  • Save joshfeck/c18bb8b76a0eb9a66e12 to your computer and use it in GitHub Desktop.
Save joshfeck/c18bb8b76a0eb9a66e12 to your computer and use it in GitHub Desktop.
Adds a new user account and sends a new user email after registering for an event. Requires EE3 and its WP user integration add-on.
<?php
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'] ) == 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' );
// Get the username
$user_info = get_userdata( $user_id );
$user_name = $user_info->user_login;
// Email the user
wp_mail( $attendee_data['email'], 'Welcome to ' . $org_options['organization'],
'Your Username: ' . $user_name . ' Your Password: ' . $password .
' You can log in and view your registrations by clicking this link: '
. wp_login_url( admin_url( 'users.php?page=my-events' ) ) );
// add this user account to the event they just registered for
if (defined('EVENTS_MEMBER_REL_TABLE')) {
require_once(EVENT_ESPRESSO_MEMBERS_DIR . "member_functions.php"); //Load Members functions
require(EVENT_ESPRESSO_MEMBERS_DIR . "user_vars.php"); //Load Members functions
if ($user_id != 0) {
event_espresso_add_user_to_event( $attendee_data['event_id'], $user_id, $attendee_data['attendee_id'] );
}
}
} // end if
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment