Support

Account

Home Forums Front-end Issues User registration with Contact Form 7 and ACF

Helping

User registration with Contact Form 7 and ACF

  • Hello,
    I try to create an user registration form with wp contact form 7 and would like to save profile’s extra field values from this form using ACF like address or phone number.
    Here is my function to create new user with wp contact form 7 but update_user_meta doesn’t work.
    Please, can you explain what’s wrong with my function.
    Thanks in advance for any help.
    K.

    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() == 'Inscription') {
    		$password = $formdata['password2'];
    		$email = $formdata['your-email'];
    		$name = $formdata['your-name'];
    		$firstname = $formdata['your-firstname'];
    		$username = $formdata['your-pseudo'];
    		$address =  $formdata['your-address'];
    		
            if ( !username_exists( $username )  && !email_exists( $email ) ) {
                $user_id = wp_create_user( $username, $password, $email );
                wp_update_user(
                    array(
                        'ID'            =>   $user_id,
                        'nickname'      =>   $username,
                        'display_name'  =>   $username,
                        'first_name'    =>   $firstname,
                        'last_name'     =>   $name,
                        'user_email'     =>  $email
                    )
                );
    			update_user_meta($user_id, $address, $_POST['fields']['field_54564ac3a47eb'] );
    
                $user = new WP_User( $user_id );
    
                // Set the user's role
                $user->set_role( 'subscriber' );
                // 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'), $user->user_login) . "\r\n";
                $message .= sprintf(__('Password: %s'), $password) . "\r\n";
                $message .= wp_login_url() . "\r\n";
                wp_mail($user->user_email, sprintf(__('[%s] Your username and password'), $blogname), $message);
            }
        }
        return $cfdata;
    }
    add_action('wpcf7_before_send_mail', 'create_user_from_registration', 1);
    add_filter('acf/pre_save_post' , 'create_user_from_registration' );
  • Just in case anyone else wanders into this post, there is a solution here

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.