1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
function wpm_create_user_form_registration( $cfdata ) { if ( ! isset( $cfdata->posted_data ) && class_exists( 'WPCF7_Submission' ) ) { // Contact Form 7 version 3.9 removed $cfdata->posted_data and now // we have to retrieve it from an API $submission = WPCF7_Submission::get_instance(); if ( $submission ) { $formdata = $submission->get_posted_data(); } } elseif ( isset( $cfdata->posted_data ) ) { // For pre-3.9 versions of Contact Form 7 $formdata = $cfdata->posted_data; } else { // We can't retrieve the form data return $cfdata; } // Check this is the user registration form if ( $cfdata->title() == 'Registration' ) { $username = $formdata['EMAIL']; $email = $formdata['EMAIL']; $password = $formdata['PASSWORD']; $fname = $formdata['FNAME']; $lname = $formdata['LNAME']; 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' => $fname . ' ' . $lname, 'display_name' => $fname . ' ' . $lname, 'first_name' => $fname, 'last_name' => $lname, 'role' => 'subscriber' ); $user_id = wp_insert_user( $userdata ); if ( ! is_wp_error( $user_id ) ) { wp_set_current_user( $user_id ); wp_set_auth_cookie( $user_id ); do_action( 'woocommerce_created_customer', $user_id ); } } } return $cfdata; } add_action( 'wpcf7_before_send_mail', 'wpm_create_user_form_registration', 1 ); |