Contact Form 7 may be a popular form plugin for WordPress. you’ll use it to make a user registration form on the front-end of your internet site. Normally Contact Form 7 will email the shape submission to the location administrator, who then has got to create the user manually within the Users section of the location admin. However, this process is often automated by hooking into the ‘wpcf7_before_send_mail’ action and creating a user from the submitted form fields.

Sample of the contact form 7 code looks like this,

<label> First name
    [text* first-name] </label>
<label> Last name
    [text* last-name] </label>
<label> Your email
    [email* your-email] </label>
<label> password
    [text* your-password] </label>
[submit "Submit"]

Contact Form 7 version 3.9 introduced a replacement API to retrieve the shape data and that I haven’t found this documented anywhere, therefore the code below may help users of three .9 who are scratching their heads.

The code below is placed in your theme’s functions.php file. you’ll get to adjust it to reflect your own user registration form title and form field names.

    if (!isset($cfdata->posted_data) && class_exists('WPCF7_Submission')) {
        // Contact Form 7 version 3.9 removed $cfdata->posted_data and now
        // we have to retrieve it from an API
        $submission = WPCF7_Submission::get_instance();
        if ($submission) {
            $formdata = $submission->get_posted_data();
        }
    } elseif (isset($cfdata->posted_data)) {
        // For pre-3.9 versions of Contact Form 7
        $formdata = $cfdata->posted_data;
    } else {
        // We can't retrieve the form data
        return $cfdata;
    }
    // Check this is the user registration form
    if ( $cfdata->title() == 'Your Registration Form Title') {
        $fname = $formdata['first-name'];
        $lname = $formdata['last-name'];
        $email = $formdata['your-email'];
        $password = $formdata['your-password'];
        // either let the user set the password or generate password 
        // or 
        // $password = wp_generate_password( 12, false );
        $name = $fname + $lname;
        $username = strtolower(str_replace(' ', '', $name));

      
        
        if ( !email_exists( $email ) ) {
            // Find an unused username
            $username_tocheck = $username;
            $i = 1;
            while ( username_exists( $username_tocheck ) ) {
                $username_tocheck = $username . $i++;
            }
            $username = $username_tocheck;
            // Create the user
            $userdata = array(
                'user_login' => $username,
                'user_pass' => $password,
                'user_email' => $email,
                'nickname' => reset($fname),
                'display_name' => $name,
                'first_name' => $fname,
                'last_name' => $lname,
                'role' => 'subscriber'
            );
            $user_id = wp_insert_user( $userdata );
            if ( !is_wp_error($user_id) ) {
                // Email login details to user
                $blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
                $message = "Welcome! Your login details are as follows:" . "rn";
                $message .= sprintf(__('Username: %s'), $username) . "rn";
                $message .= sprintf(__('Password: %s'), $password) . "rn";
                $message .= wp_login_url() . "rn";
                wp_mail($email, sprintf(__('[%s] Your username and password'), $blogname), $message);
            }
        }
    }
    return $cfdata;
}
add_action('wpcf7_before_send_mail', 'create_user_from_registration', 1);

Notes:

  1. If the email address is already associated with an existing user, a new one will not be created.
  2. You can pass additional fields such as website URL to wp_insert_user – see the documentation.
  3. If you need to manually approve the user before giving them access, you can create a ‘pending’ role with limited capabilities, and set the script to allocate that role to the new user. You will then have to manually allocate the user to their full role in the site admin once approved.
  4. Justin Tadlock’s Members plugin is useful for managing WordPress Roles from the admin.

Categorized in: