Skip to content

Instantly share code, notes, and snippets.

@apintocr
Last active April 24, 2018 20:10
Show Gist options
  • Select an option

  • Save apintocr/ba28dc266750910b5e7a039629e8e6a5 to your computer and use it in GitHub Desktop.

Select an option

Save apintocr/ba28dc266750910b5e7a039629e8e6a5 to your computer and use it in GitHub Desktop.

Revisions

  1. apintocr revised this gist May 17, 2017. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions filterContactFormRecipient.php
    Original file line number Diff line number Diff line change
    @@ -7,8 +7,7 @@
    * You can add some conditional checks to make sure the data is correct.
    *
    * @author António Pinto <[email protected]>
    * @license http://opensource.org/licenses/gpl-license.php GNU Public License
    * @ref https://gitlab.com/snippets/1662384
    * @link https://gitlab.com/snippets/1662384
    *
    * @param \WPCF7_ContactForm $contactForm
    */
  2. apintocr created this gist May 17, 2017.
    33 changes: 33 additions & 0 deletions filterContactFormRecipient.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    <?php
    /**
    * Contact Form 7 - Conditionally Chance Recipient to Post Meta
    *
    * This function gets the current post id and grabs the desired meta to use
    * it on the submited form data to change the recipient email conditionally.
    * You can add some conditional checks to make sure the data is correct.
    *
    * @author António Pinto <[email protected]>
    * @license http://opensource.org/licenses/gpl-license.php GNU Public License
    * @ref https://gitlab.com/snippets/1662384
    *
    * @param \WPCF7_ContactForm $contactForm
    */
    function filterContactFormRecipient($contactForm)
    {
    // Get the post id from the existing WPCT7 special mail tags.
    $postID = \wpcf7_special_mail_tag('', '_post_id', '');

    // Get the desired post meta.
    $emailAddress = get_post_meta($postID, 'company_email', true);

    // Get the submited form email data.
    $mailData = $contactForm->prop('mail');

    // Change the 'recipient' (or any other field).
    $mailData['recipient'] = $emailAddress;

    // Update/set the form properties.
    $contactForm->set_properties(['mail' => $mailData]);
    }

    add_action('wpcf7_before_send_mail', __NAMESPACE__ . '\\filterContactFormRecipient');