Search code examples
phpwordpresscontact-form-7

Get CF7 input values to another form on another page


I need to get the values of this form diplayed on another form on another page.

PS. The second form is not CF7. I believe Contact Form 7 Dynamic Text Extension won't work.

I have already tried the following.

function save_cf7_data($cf)
{
    $submission = WPCF7_Submission::get_instance();

    // Get the post data and other post meta values.
    if ($submission) {
        session_start();
        $posted_data = $submission->get_posted_data();
        // Encode the data in a new array in JSON format
        $data = json_encode(array(
            "Property_Address" => "{$posted_data["Property_Address"]}",
            "Email" => "{$posted_data["Email"]}",
            "Phone" => "{$posted_data["Phone"]}",

        ));


        // Finally send the data to your custom endpoint
        $ch = curl_init(get_stylesheet_directory_uri() . '/second-form.php');
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); //Optional timeout value
        curl_setopt($ch, CURLOPT_TIMEOUT, 5); //Optional timeout value
        $result = curl_exec($ch);
        curl_close($ch);

        return $result;
    }
    }
    add_action('wpcf7_before_send_mail', 'save_cf7_data');

Question One: How to pass the second form URL on curl_init()?

Question Two: How to get the variables on the second form php file?


Solution

  • You can save data in session:

    on init hook you need start session if session not exist

    add_action( 'init', 'mx_session_start' );
    function 'mx_session_start'() {
        if ( empty( $_SESSION ) ) {
            session_start();
        }
    }
    

    on you hook in example save data in session

    $_SESSION['cf7'] = $data; //all your needed data.
    

    Get your data in another form use $_SESSION['cf7']

    or change cf7-form on simple form with action on your "another" page