Ready to get started?Download WordPress


Contact Form 7
[resolved] Modify posted data before sending (8 posts)

  1. e
    Posted 1 year ago #

    I am trying to edit the $wpcf7 object data but it ignores it, this is the code:

    function wpcf7_modify_this(&$wpcf7_data) {
      $wpcf7_data->mail->recipient = "Mark <mark@gmail.com>";
    add_action("wpcf7_before_send_mail", "wpcf7_modify_this");

    I have been able to cancel emails by adding:
    $wpcf7_data->skip_mail = true;

    But changing any other parameter such as the recipient doesn't work.

    Any ideas??


  2. e
    Posted 1 year ago #

    Ok, finally got it. It turns out the WPCF7 thing is an Object with Arrays inside so it should be called like:
    $wpcf7_data->mail['recipient'] = "Mark <mark@gmail.com>";

    Hope that helps :)

  3. strigga
    Posted 1 year ago #

    what would you like to do with the posted data?

  4. e
    Posted 1 year ago #

    Hi strigga,

    You can do pretty much anything, some examples:

    1) if the user typed in the word "support" or "help" you could change the recipient to support@company.com, but if it found "business enquiry" forward it to sales@company.com.

    2) Same goes for a City field, if the user is in London you could send the email to the London branch.

    3) Or if the form was submitted on a weekend, when the offices are closed, you can send a confirmation email like "Thanks for your email, unfortunately we are out of the office, will get back to you on Monday".

    4) If there is a budget field and the user types in "1,000" then forward it to sales@company.com, but if the user types in "10,000,000" then the boss will probably want to deal with that juicy client himself.

    There's plenty of examples really, the idea is to have fewer forms on the site (or even just one) which then through some smart coding can do custom actions or change certain things.

  5. MikeHarris
    Posted 1 year ago #

    Very handy, cheers mate

  6. toomaat
    Posted 1 year ago #

    @ e

    What to do when you have multiple forms (contact forms and apply forms) on you're website? How do you determine when this function must be executed? My custom before mail send function should only be executed with the apply forms.

  7. e
    Posted 1 year ago #

    @ toomaat

    Each form has an ID: $wpcf7_data->id so once you know the ID of the Apply forms you can put a conditional if($wpcf7_data->id == 123) and take action only for that form

  8. toomaat
    Posted 1 year ago #


    In the mean time I also found the solution 'how to handle with multiple forms".

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic