• Hi,

    I’m trying to use Contact Form 7 to register my users in BuddyPress. I’ve tried two

    With a plugin (Frontend Registration – Contact Form 7)
    Some code
    I’m trying to use this forum to register users. I’m using:

    • wP 4.9.5
    • Contact Form 5.0.1
    • Frontend Registration – Contact Form 7
    • BuddyPress 2.9.4 (Extended Profiles activated, I need this)

    Plugin

    Steps I followed:

    1. I’ve created my contact form “Register Form” and I’ve activated Registration Settings
    2. I’ve created my page “Registration” with “Register Form” on it
    3. At this point “Registration page” works ok (shows “Register Form”)
    4. Then I go to BuddyPress Settings and in Register Page select “Registration page”
    5. When I go to “Registration page”, all the content has been overwrite with BuddyPress registration fields 

    Some code

    function create_user_from_registration($cfdata) {
        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') {
            $password = wp_generate_password( 12, false );
            $email = $formdata['form-email-field'];
            $name = $formdata['form-name-field'];
            // Construct a username from the user's name
            $username = strtolower(str_replace(' ', '', $name));
            $name_parts = explode(' ',$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($name_parts),
                    'display_name' => $name,
                    'first_name' => reset($name_parts),
                    'last_name' => end($name_parts),
                    '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:" . "\r\n";
                    $message .= sprintf(__('Username: %s'), $username) . "\r\n";
                    $message .= sprintf(__('Password: %s'), $password) . "\r\n";
                    $message .= wp_login_url() . "\r\n";
                    wp_mail($email, sprintf(__('[%s] Your username and password'), $blogname), $message);
                }
            }
        }
        return $cfdata;
    }
    add_action('wpcf7_before_send_mail', 'create_user_from_registration', 1);

    Source (2014)

    I’m not sure if I’m doing something wrong or simply this code it’s not working anymore. I have some custom code to create CPT from a CF7. Maybe this is a problem?

    Any idea about how to do this (without using a paid plugin)?

    Thank you

    • This topic was modified 6 years, 10 months ago by orero.
  • The topic ‘Buddy Press Registration’ is closed to new replies.