Search code examples
wordpresscontact-form-7

Contact Form 7 set variable before send hook


I would like to set a quote number when an email is submitted. Here is my the variable in the form i would like to set.

[hidden frnum id:frnum]

in the functions.php

function wpcf7_setup_quotenum($WPCF7_ContactForm) {
if ($WPCF7_ContactForm->id() == '3550') {
     //Get current form
    $wpcf7      = WPCF7_ContactForm::get_current();

    // get current SUBMISSION instance
    $submission = WPCF7_Submission::get_instance();
    // Ok go forward
    if ($submission) {
        $_POST['frnum'] = 'test';
    }

}
}

and this is the email code to get the number.

[your-name] thought you would be interested in this Forklift Rental Quote#:FR[frnum]

I will eventually have code to set the number to a unique number, im just trying to get something to show in the email.

Thanks in advance for pointing me in the right direction.


Solution

  • For anyone in the future, this is what i ended up with:

    if ($WPCF7_ContactForm->id() == '3550') {
         //Get current form
        $wpcf7      = WPCF7_ContactForm::get_current();
    
        // get current SUBMISSION instance
        $submission = WPCF7_Submission::get_instance();
        // Ok go forward
        if ($submission) {
            // do some replacements in the cf7 email body
            $mail         = $wpcf7->prop('mail');
            // Find/replace the "[frnum]" tag as defined in your CF7 email body
            // and add changes name
            $mail['body'] = str_replace('[frnum]', 'test', $mail['body']);
            //$mail['body'] = '[frnum]' => 'test';
    
            // Save the email body
            $wpcf7->set_properties(array(
                "mail" => $mail
            ));
    
            // return current cf7 instance
            return $wpcf7;
    
        }
    
    }