I wanted to use the clients existing Contact Form 7 WordPress plugin. Each Cardiology member has their own profile page. Their email is in a native custom field. I wanted to send the contact form to each individual member. To do this I would have to dynamically populate the To: field with the email address from the custom field. I worked on this for hours before consulting Stack Overflow.
Within the contact form, it doesn’t let you get the
https://stackoverflow.com/questions/66881074/wordpress-contact-form-7-insert-dynamic-to-address-based-on-meta-data$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 that before_send_mail is an “ACTION” and not a “FILTER”
This worked in my functions.php file:
function wpcf7_before_send_mail_function($contact_form, &$abort, $submission){
// This gets your post_id
$post_id = $submission->get_meta('container_post_id');
$properties = $contact_form->get_properties();
$properties['mail']['recipient'] = get_post_meta( $post_id, 'women_email', true);
$contact_form->set_properties($properties);
}
add_action( 'wpcf7_before_send_mail', 'wpcf7_before_send_mail_function', 10, 3 );