Advertisement
Guest User

Untitled

a guest
Jul 12th, 2016
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.95 KB | None | 0 0
  1. //contact form 7 submissions create user
  2. function create_user_from_registration($cfdata) {
  3.     // set up submission object
  4.         $submission = WPCF7_Submission::get_instance();
  5.         $formdata = $submission->get_posted_data();
  6.     // Check this is the user registration form
  7.     if ( $cfdata->title() == 'Registration Form_copy' || $cfdata->title() == 'Registration Form In House Programmes') {
  8.     //submitted fields
  9.         $email = $formdata['email'];
  10.         $first_name = $formdata['firstname'];
  11.         $surname = $formdata['surname'];
  12.         $full_name = $first_name . ' ' . $surname;
  13.         $programme = $formdata['prog_name'];
  14.         $programme_id = $formdata['prog_id'];
  15.         $programme_end = $formdata['prog_date'];
  16.         $password = $first_name.'_'.$surname.'_'.$programme_end;
  17.         $coach = $formdata['tname'];
  18.        
  19.         // Construct a username from the user's name
  20.         $username = $full_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.                 'nickname' => $first_name,
  35.                 'display_name' => $full_name,
  36.                 'first_name' => $first_name,
  37.                 'last_name' => $surname,
  38.                 'role' => 'subscriber'
  39.             );
  40.             $user_id = wp_insert_user( $userdata );
  41.             //add programme meta
  42.             // programme name
  43.             add_user_meta( $user_id,'programme', $programme );
  44.            
  45.             // programme id
  46.              add_user_meta( $user_id,'programme_id', $programme_id );
  47.            
  48.             //end date meta
  49.             add_user_meta( $user_id,'programme_end_date', $programme_end );
  50.            
  51.             //coach meta
  52.             update_field('coach', $coach, 'user_'.$user_id);
  53.            
  54.             if ( !is_wp_error($user_id) ) {
  55.                 // Email login details to user
  56.                 $blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
  57.                 $message = sprintf(__('Thank you for sigining up for the Dialogix Programme - %s.'), $programme) . "\r\n";
  58.                 $message .= "One of our team will be in touch shortly to discuss the next steps." . "\r\n" . "You will need the following login details to access our online course" . "\r\n";
  59.                 $message .= sprintf(__('Username: %s'), $username) . "\r\n";
  60.                 $message .= sprintf(__('Password: %s'), $password) . "\r\n";
  61.                 $message .= wp_login_url() . "\r\n";
  62.                 wp_mail('email@domain.com', sprintf(__('Thank you for registering for a %s progamme.'), $blogname), $message);
  63.             }
  64.         }
  65.     }
  66.     return $cfdata;
  67. }
  68. add_action('wpcf7_before_send_mail', 'create_user_from_registration', 1);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement