I've been attempting to change CF7 form fields dynamically without using Contact Form 7 Dynamic Text Extension. I've seen a ton of articles on how to get posted data, just not on how I can overwrite existing values. My goal is to dynamically change the file attachment and add other meta-data associated with each post. Can this be done? Thank you!
Here's what I have so far:
function wpcf7_custom_before_send(&$cf7) {
    if ( $cf7->id == 4 ) {
        $submission = WPCF7_Submission::get_instance();
        if ( $submission ) {
            $data =& $submission->get_posted_data();
            // how do I overwrite posted data?
        }
    }
}
add_action("wpcf7_before_send_mail", "wpcf7_custom_before_send");
                You can use my code to do this. Some explanations for your code:
1) Since id $cf7->id property is no longer accessible. Use id() method instead $cf7->id().
2) No need use & for callback $cf7 and $submission. Use for this return.
add_action("wpcf7_before_send_mail", "wpcf7_do_something");
function wpcf7_do_something($WPCF7_ContactForm)
{
    if (224 == $WPCF7_ContactForm->id()) {
        //Get current form
        $wpcf7      = WPCF7_ContactForm::get_current();
        // get current SUBMISSION instance
        $submission = WPCF7_Submission::get_instance();
        // Ok go forward
        if ($submission) {
            // get submission data
            $data = $submission->get_posted_data();
            // nothing's here... do nothing...
            if (empty($data))
                return;
            // extract posted data for example to get name and change it
            $name         = isset($data['your-name']) ? $data['your-name'] : "";
            // do some replacements in the cf7 email body
            $mail         = $wpcf7->prop('mail');
            // Find/replace the "[your-name]" tag as defined in your CF7 email body
            // and add changes name
            $mail['body'] = str_replace('[your-name]', $name . '-tester', $mail['body']);
            // Save the email body
            $wpcf7->set_properties(array(
                "mail" => $mail
            ));
            // return current cf7 instance
            return $wpcf7;
        }
    }
}
That is all, we are changed some tags, and send email with modified tag ;-)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With