Search code examples
phpwordpresscontact-form-7

Contact Form 7 : send default value to mail if textarea empty


[your-name] I have a contact form7 3.7 with a textarea field as :

<p>Message (optional)<br />
[textarea my-message]</p>

And Mail Format is :

From :    [your-name] | [your-email]
Message : [my-message]

Now, currently the textarea is empty.
if user does not fills any data in message box, the mail is sent with empty field.
How to send "None" value by default if the textarea is empty ?


Solution

  • This took a while to find and could use some work yet, but try this in your functions.php file:

    add_action("wpcf7_posted_data", "wpcf7_modify_this");
    function wpcf7_modify_this($posted_data) {
    
        // if user leaves the message area blank, set to "None"
        if ($posted_data['my-message'] == "")
            $posted_data['my-message'] = "None";
    
        // generic example
        if ( /* some logic */ )
            $posted_data['/* cf7-field-name */'] = "ValueToEmail";
    
        return $posted_data;
    }
    

    One thing to note here is that validation occurs prior to this point, so if you're allowing users to enter a blank value, don't make it required.