Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //contact form 7 submissions create user
- function create_user_from_registration($cfdata) {
- //setup submission object
- $submission = WPCF7_Submission::get_instance();
- $formdata = $submission->get_posted_data();
- if ( $cfdata->title() == 'Registration Form_copy') {
- //submitted fields
- $email = $formdata['email'];
- $first_name = $formdata['firstname'];
- $surname = $formdata['surname'];
- $full_name = $first_name . ' ' . $surname;
- $programme = $formdata['prog_name'];
- $programme_id = $formdata['prog_id'];
- $programme_end = $formdata['prog_date'];
- $password = $first_name.'_'.$surname.'_'.$programme_end;
- $coach = $formdata['tname'];
- // Construct a username from the user's name
- $username = $full_name;
- if ( !email_exists( $email ) ) {
- // Find an unused username
- $username_tocheck = $username;
- $i = 1;
- while ( username_exists( $username_tocheck ) ) {
- $username_tocheck = $username . ' ' . $i++;
- }
- $username = $username_tocheck;
- // Create the user
- $userdata = array(
- 'user_login' => $username,
- 'user_pass' => $password,
- 'user_email' => $email,
- 'nickname' => $first_name,
- 'display_name' => $full_name,
- 'first_name' => $first_name,
- 'last_name' => $surname,
- 'role' => 'subscriber'
- );
- $user_id = wp_insert_user( $userdata );
- //add programme meta
- // programme name
- add_user_meta( $user_id,'programme', $programme );
- // programme id
- add_user_meta( $user_id,'programme_id', $programme_id );
- //end date meta
- add_user_meta( $user_id,'programme_end_date', $programme_end );
- update_field('coach', $coach, 'user_'.$user_id);
- if ( !is_wp_error($user_id) ) {
- // Email login details to user
- $blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
- $message = sprintf(__('Thank you for sigining up for the Dialogix Programme - %s.'), $programme) . "\r\n";
- $message .= "One of our team will be in touch shortly to discuss the next steps." . "\r\n" . "You will need the following login details to access our online course" . "\r\n";
- $message .= sprintf(__('Username: %s'), $username) . "\r\n";
- $message .= sprintf(__('Password: %s'), $password) . "\r\n";
- $message .= wp_login_url() . "\r\n";
- wp_mail(xxxx@xxxx', sprintf(__('Thank you for registering for a %s progamme.'), $blogname), $message);
- }
- }
- }
- return $cfdata;
- }
- add_action('wpcf7_before_send_mail', 'create_user_from_registration', 1);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement