Advertisement
JustUsAgency

CF7 User reg

Feb 7th, 2019
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.31 KB | None | 0 0
  1. function create_user_from_registration($cfdata) {
  2.     if ( $cfdata->title() == 'Quick Register') {
  3.     $checkbox = isset($formdata['checkbox-register']);
  4.     if ($checkbox) {
  5.     if (!isset($cfdata->posted_data) && class_exists('WPCF7_Submission')) {
  6.         // Contact Form 7 version 3.9 removed $cfdata->posted_data and now
  7.         // we have to retrieve it from an API
  8.         $submission = WPCF7_Submission::get_instance();
  9.         if ($submission) {
  10.             $formdata = $submission->get_posted_data();
  11.         }
  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() == 'Quick Register') {
  18.         $password = wp_generate_password( 12, false );
  19.         $email = $formdata['email'];
  20.         $name = $formdata['first-name'] ['last-name'];
  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' => 'candidate'
  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 to Harper Construction Recruitment! 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. }
  58.         else {
  59. //disable registration
  60. }
  61.  
  62.     }
  63. add_action('wpcf7_before_send_mail', 'create_user_from_registration', 1);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement