Advertisement
Guest User

Untitled

a guest
Jul 15th, 2019
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.40 KB | None | 0 0
  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() == 'Registration: Retailer') {
  18. $password = wp_generate_password( 12, false );
  19. $email = $formdata['email'];
  20. $name = $formdata['username'];
  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' => $formdata['company'],
  39. 'first_name' => $formdata['firstname'],
  40. 'last_name' => $formdata['lastname'],
  41. 'role' => 'customer'
  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 = "Hello! We have received your registration request and is pending approval (be sure to save this email). We take pride in verifying that you are a legitimate business before approving your account. Your login details are as follows:" . "rn";
  48. $message .= sprintf(__('Username: %s'), $username) . "rn";
  49. $message .= sprintf(__('Password: %s'), $password) . "rn";
  50. $message .= home_url() . "rn";
  51. wp_mail($email, sprintf(__('[%s] Your username and password pending approval: SAVE THIS EMAIL'), $blogname), $message);
  52. }
  53. }
  54. }
  55. return $cfdata;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement