Advertisement
Guest User

Untitled

a guest
Jul 12th, 2016
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.83 KB | None | 0 0
  1. //contact form 7 submissions create user
  2. function create_user_from_registration($cfdata) {
  3.  
  4.         //setup submission object
  5.         $submission = WPCF7_Submission::get_instance();
  6.         $formdata = $submission->get_posted_data();
  7.     if ( $cfdata->title() == 'Registration Form_copy') {
  8.        
  9.         //submitted fields
  10.         $email = $formdata['email'];
  11.         $first_name = $formdata['firstname'];
  12.         $surname = $formdata['surname'];
  13.         $full_name = $first_name . ' ' . $surname;
  14.         $programme = $formdata['prog_name'];
  15.         $programme_id = $formdata['prog_id'];
  16.         $programme_end = $formdata['prog_date'];
  17.         $password = $first_name.'_'.$surname.'_'.$programme_end;
  18.         $coach = $formdata['tname'];
  19.    
  20.         // Construct a username from the user's name
  21.         $username = $full_name;
  22.  
  23.         if ( !email_exists( $email ) ) {
  24.             // Find an unused username
  25.             $username_tocheck = $username;
  26.             $i = 1;
  27.             while ( username_exists( $username_tocheck ) ) {
  28.                 $username_tocheck = $username . ' ' .  $i++;
  29.             }
  30.             $username = $username_tocheck;
  31.  
  32.             // Create the user
  33.             $userdata = array(
  34.                 'user_login' => $username,
  35.                 'user_pass' => $password,
  36.                 'user_email' => $email,
  37.                 'nickname' => $first_name,
  38.                 'display_name' => $full_name,
  39.                 'first_name' => $first_name,
  40.                 'last_name' => $surname,
  41.                 'role' => 'subscriber'
  42.             );
  43.  
  44.             $user_id = wp_insert_user( $userdata );
  45.  
  46.             //add programme meta
  47.             // programme name
  48.             add_user_meta( $user_id,'programme', $programme );
  49.            
  50.             // programme id
  51.              add_user_meta( $user_id,'programme_id', $programme_id );
  52.            
  53.             //end date meta
  54.             add_user_meta( $user_id,'programme_end_date', $programme_end );
  55.            
  56.             update_field('coach', $coach, 'user_'.$user_id);
  57.            
  58.             if ( !is_wp_error($user_id) ) {
  59.                 // Email login details to user
  60.                 $blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
  61.                 $message = sprintf(__('Thank you for sigining up for the Dialogix Programme - %s.'), $programme) . "\r\n";
  62.                 $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";
  63.                 $message .= sprintf(__('Username: %s'), $username) . "\r\n";
  64.                 $message .= sprintf(__('Password: %s'), $password) . "\r\n";
  65.                 $message .= wp_login_url() . "\r\n";
  66.                 wp_mail(xxxx@xxxx', sprintf(__('Thank you for registering for a %s progamme.'), $blogname), $message);
  67.            }
  68.        }
  69.    }
  70.    return $cfdata;
  71. }
  72. add_action('wpcf7_before_send_mail', 'create_user_from_registration', 1);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement