Skip to content

Instantly share code, notes, and snippets.

@apintocr
Last active April 24, 2018 20:10
Show Gist options
  • Save apintocr/ba28dc266750910b5e7a039629e8e6a5 to your computer and use it in GitHub Desktop.
Save apintocr/ba28dc266750910b5e7a039629e8e6a5 to your computer and use it in GitHub Desktop.
Contact Form 7 - Conditionally Chance Recipient to Post Meta
<?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 <apinto@vanguardly.com>
* @link 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');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment