GosuMania

Untitled

Jul 23rd, 2019
176
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. } else {
  10. // We can't retrieve the form data
  11. return $cfdata;
  12. }
  13. // Check this is the user registration form
  14. if ( $cfdata->title() == 'Registration Form') {
  15. $password = wp_generate_password( 12, false );
  16. $email = $formdata['form-email-field'];
  17. $name = $formdata['form-name-field'];
  18. // Construct a username from the user's name
  19. $username = strtolower(str_replace(' ', '', $name));
  20. $name_parts = explode(' ',$name);
  21. if ( !email_exists( $email ) ) {
  22. // Find an unused username
  23. $username_tocheck = $username;
  24. $i = 1;
  25. while ( username_exists( $username_tocheck ) ) {
  26. $username_tocheck = $username . $i++;
  27. }
  28. $username = $username_tocheck;
  29. // Create the user
  30. $userdata = array(
  31. 'user_login' => $username,
  32. 'user_pass' => $password,
  33. 'user_email' => $email,
  34. 'role' => 'subscriber'
  35. );
  36. $user_id = wp_insert_user( $userdata );
  37. if ( !is_wp_error($user_id) ) {
  38. // Email login details to user
  39. $blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
  40. $message = "Welcome! Your login details are as follows:" . "\r\n";
  41. $message .= sprintf(__('Username: %s'), $username) . "\r\n";
  42. $message .= sprintf(__('Password: %s'), $password) . "\r\n";
  43. $message .= wp_login_url() . "\r\n";
  44. wp_mail($email, sprintf(__('[%s] Your username and password'), $blogname), $message);
  45. }
  46. }
  47. }
  48. return $cfdata;
  49. }
  50. add_action('wpcf7_before_send_mail', 'create_user_from_registration', 1);
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×