I would like to send a confirmation email to a dynamic recipient. This is in the functions.php file. The below code works, but when I try to replace the static email with a variable, it has an error. How do I properly query the post_meta
women_email and dynamically insert it in place of the static gmail address?
Example page: https://www.ohioacc.org/women/sandra-nichols/
function wpcf7_before_send_mail_function( $contact_form, $abort, $submission ) {
$women_email = get_post_meta( get_the_ID(), 'women_email', true);
//$dynamic_email = $women_email; THIS DOES NOT WORK
$dynamic_email = "[email protected]"; //THIS WORKS
$properties = $contact_form->get_properties();
$properties['mail']['recipient'] = $dynamic_email;
$contact_form->set_properties($properties);
return $contact_form;
}
add_filter( 'wpcf7_before_send_mail', 'wpcf7_before_send_mail_function', 10, 3 );
3
Answers
Contact Form 7 allows you to create an auto-responder, by selecting Mail(2), which is located under the Mail tab. You’ll need to edit the relevant form:
Contact Form 7 Mail Tab,
Contact Form 7 Mail(2).
You can then use your form’s variable to populate the "To" field:
Contact Form 7 auto-responder field
Are you certain you’re passing the post ID in correctly?
get_the_ID()
needs proper context, in this case, the WP LoopI’m assuming this is a function in your functions.php.
This should do the trick.
Within the contact form, it doesn’t let you get the
$post->ID
of the parent post. This data is stored in the form’s meta. It’s a hidden field passed in the form.If you inspect your form from the front end, you’ll see this For example:
<input type="hidden" name="_wpcf7_container_post" value="2730">
To get this, you access the
$submission->get_meta('$value')
method.This will work for you, given that the
women_email
is properly formatted as an email address. Also note thatbefore_send_mail
is an "ACTION" and not a "FILTER"Below is not tested, but should work. Put this in functions.php