17

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");
Krupal Panchal
  • 1,553
  • 2
  • 13
  • 26
DeFeNdog
  • 1,156
  • 1
  • 12
  • 25

2 Answers2

49

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 ;-)

Brotheryura
  • 1,158
  • 13
  • 21
  • I'll come back and mark it as an answer as soon as I get a chance to test it. I've up-voted for the time being. Thanks! – DeFeNdog Nov 06 '14 at 21:47
  • Thanks! This helped me decode some html before it was sent over – Ben May 18 '18 at 05:56
  • 1
    This works fine for the email, but what about plugins like Flamingo that store the data fields? Those still appear unmodified. – AuRise Jul 03 '18 at 13:45
  • how can one change the subject dynamically or get the posted subject? for example, I can see email content is `$mail['body']`. What about the subject – Karue Benson Karue Jun 01 '20 at 17:24
  • @KarueBensonKarue try to see what you have in `$mail['subject'] ` and then change it like you want. – Brotheryura Jun 03 '20 at 00:23
1

As I needed to modify form receivers based on ACF fields, here is a copy and paste solution which is based on @Brotheryura code.

It allows you to modify the recipient of the email dynamically without having any hidden fields in the front end. Simpy put it in your templates functions.php and replace the $recipient = ... part with your own function or code to get the new receiver.

add_action("wpcf7_before_send_mail", "wpcf7_change_recipient");
function wpcf7_change_recipient($WPCF7_ContactForm)
{
    $wpcf7      = WPCF7_ContactForm::get_current();
    $submission = WPCF7_Submission::get_instance();

    //some little magic to get the referers ID
    $recipient  = get_field('mail', url_to_postid(wp_get_referer()));

    if (!empty($recipient))
    {
        if ($submission)
        {
            $data = $submission->get_posted_data();

            // nothing's here... do nothing...
            if (empty($data))
                return;

            // do some replacements in the cf7 email body
            $mail              = $wpcf7->prop('mail');
            $mail['recipient'] = $recipient;

            // Save the email body
            $wpcf7->set_properties(array(
                "mail" => $mail
            ));

            // return current cf7 instance
            return $wpcf7;
        }
    }
}
christian
  • 180
  • 7