Advertisement
Guest User

Untitled

a guest
Mar 5th, 2019
293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.95 KB | None | 0 0
  1. //Register User Via CF7
  2.  
  3. function create_user_from_registration($cfdata) {
  4.     if (!isset($cfdata->posted_data) && class_exists('WPCF7_Submission')) {
  5.         // Contact Form 7 version 3.9 removed $cfdata->posted_data and now
  6.         // we have to retrieve it from an API
  7.         $submission = WPCF7_Submission::get_instance();
  8.         if ($submission) {
  9.             $formdata = $submission->get_posted_data();
  10.         }
  11.     } elseif (isset($cfdata->posted_data)) {
  12.         // For pre-3.9 versions of Contact Form 7
  13.         $formdata = $cfdata->posted_data;
  14.     } else {
  15.         // We can't retrieve the form data
  16.         return $cfdata;
  17.     }
  18.     // Check this is the user registration form
  19.     if ( $cfdata->title() == 'Front Page Register') {
  20.         $password = wp_generate_password( 12, false );
  21.         $email = $formdata['best-email'];
  22.         $name = $formdata['first-name'];
  23.         // Construct a username from the user's name
  24.         $username = strtolower(str_replace(' ', '', $name));
  25.         $name_parts = explode(' ',$name);
  26.         if ( !email_exists( $email ) ) {
  27.             // Find an unused username
  28.             $username_tocheck = $username;
  29.             $i = 1;
  30.             while ( username_exists( $username_tocheck ) ) {
  31.                 $username_tocheck = $username . $i++;
  32.             }
  33.             $username = $username_tocheck;
  34.             // Create the user
  35.             $userdata = array(
  36.                 'user_login' => $username,
  37.                 'user_pass' => $password,
  38.                 'user_email' => $email,
  39.                 'nickname' => reset($name_parts),
  40.                 'display_name' => $name,
  41.                 'first_name' => reset($name_parts),
  42.                 'last_name' => end($name_parts),
  43.                 'role' => 'subscriber'
  44.             );
  45.             $user_id = wp_insert_user( $userdata );
  46.             if ( !is_wp_error($user_id) ) {
  47.                 // Email login details to user
  48.                 $blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
  49.                 $message = "Welcome! Your login details are as follows:" . "\r\n";
  50.                 $message .= sprintf(__('Username: %s'), $username) . "\r\n";
  51.                 $message .= sprintf(__('Password: %s'), $password) . "\r\n";
  52.                 $message .= wp_login_url() . "\r\n";
  53.                 wp_mail($email, sprintf(__('[%s] Your username and password'), $blogname), $message);
  54.             }
  55.         }
  56.     }
  57.     return $cfdata;
  58. }
  59. add_action('wpcf7_before_send_mail', 'create_user_from_registration', 1);
  60.  
  61. function auto_login_new_user( $user_id ) {
  62.     wp_set_current_user($user_id);
  63.     wp_set_auth_cookie($user_id);
  64.     $user = get_user_by( 'id', $user_id );
  65.     do_action( 'wp_login', $user->user_login );//`[Codex Ref.][1]
  66.     wp_redirect( '/about/' ); // You can change home_url() to the specific URL,such as "wp_redirect( 'http://www.wpcoke.com' )";
  67.     exit;
  68. }
  69. add_action( 'user_register', 'auto_login_new_user', 2);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement