Guest User

Untitled

a guest
Sep 4th, 2020
376
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function create_user_from_registration($cfdata) {
  2.     if (!isset($cfdata->posted_data) && class_exists('WPCF7_Submission')) {
  3.         // Contact Form 7 version 3.9 removed $cfdata->posted_data and now
  4.         // we have to retrieve it from an API
  5.         $submission = WPCF7_Submission::get_instance();
  6.         if ($submission) {
  7.             $formdata = $submission->get_posted_data();
  8.         }
  9.     } elseif (isset($cfdata->posted_data)) {
  10.         // For pre-3.9 versions of Contact Form 7
  11.         $formdata = $cfdata->posted_data;
  12.     } else {
  13.         // We can't retrieve the form data
  14.         return $cfdata;
  15.     }
  16.     // Check this is the user registration form
  17.     if ( $cfdata->title() == 'Your Registration Form Title') {
  18.         $password = wp_generate_password( 12, false );
  19.         $email = $formdata['form-email-field'];
  20.         $name = $formdata['form-name-field'];
  21.         // Construct a username from the user's name
  22.         $username = strtolower(str_replace(' ', '', $name));
  23.         $name_parts = explode(' ',$name);
  24.         if ( !email_exists( $email ) ) {
  25.             // Find an unused username
  26.             $username_tocheck = $username;
  27.             $i = 1;
  28.             while ( username_exists( $username_tocheck ) ) {
  29.                 $username_tocheck = $username . $i++;
  30.             }
  31.             $username = $username_tocheck;
  32.             // Create the user
  33.             $userdata = array(
  34.                 'user_login' => $username,
  35.                 'user_pass' => $password,
  36.                 'user_email' => $email,
  37.                 'nickname' => reset($name_parts),
  38.                 'display_name' => $name,
  39.                 'first_name' => reset($name_parts),
  40.                 'last_name' => end($name_parts),
  41.                 'role' => 'subscriber'
  42.             );
  43.             $user_id = wp_insert_user( $userdata );
  44.             if ( !is_wp_error($user_id) ) {
  45.                 // Email login details to user
  46.                 $blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
  47.                 $message = "Welcome! Your login details are as follows:" . "\r\n";
  48.                 $message .= sprintf(__('Username: %s'), $username) . "\r\n";
  49.                 $message .= sprintf(__('Password: %s'), $password) . "\r\n";
  50.                 $message .= wp_login_url() . "\r\n";
  51.                 wp_mail($email, sprintf(__('[%s] Your username and password'), $blogname), $message);
  52.             }
  53.         }
  54.     }
  55.     return $cfdata;
  56. }
  57. add_action('wpcf7_before_send_mail', 'create_user_from_registration', 1);
RAW Paste Data