How to use post meta to set mail recipient dynamically?
-
I’m trying to use the ‘wpcf7_before_send_mail’ filter to dynamically change the recipient of a CF7 contact form. The recipient e-mail address should come from a custom field, specifically one from the Advanced Custom Fields (ACF) plugin. If the custom field is not filled, then the regular recipient address (from the CF7 form) should be used. I feel like the code I have is pretty close, but it’s not working: It always still sends to the regular recipient. And since I’m using AJAX for the CF7 form (as is the default), I’m having trouble debugging.
Below is my code. Can anyone give me any pointers as to what may be going wrong?
// Set the recipient of the question form to the e-mail address of the connected person function set_question_form_recipient(&$WPCF7_ContactForm) { // Check if it's the right form if (102 == $WPCF7_ContactForm->id()) : //Get current form and submission instance $wpcf7 = WPCF7_ContactForm::get_current(); $submission = WPCF7_Submission::get_instance(); if ($submission) : // Get submission data $data = $submission->get_posted_data(); // nothing's here... do nothing... if (empty($data)) { return; } // Get the post ID from the unit_tag $unit_tag = $submission->get_meta( 'unit_tag' ); $explode_unit_tag = explode("-", $unit_tag); $post_id = str_replace("p", "", $explode_unit_tag[2]); // Get the connected persons $connected_persons = get_field('connected_persons', $post_id); if ($connected_persons) : // Get the e-mail address $person_id = $connected_persons[0]->ID; $person_email = get_field("person_email", $person_id); if ($person_email) : // Set the recipient $mail = $wpcf7->prop('mail'); $mail['recipient'] = $person_email; $wpcf7->set_properties(array( "mail" => $mail )); endif; endif; // return current cf7 instance return $wpcf7; endif; endif; } add_filter('wpcf7_before_send_mail', 'set_question_form_recipient');
Viewing 6 replies - 1 through 6 (of 6 total)
Viewing 6 replies - 1 through 6 (of 6 total)
- The topic ‘How to use post meta to set mail recipient dynamically?’ is closed to new replies.