skip to Main Content

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


  1. 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

    Login or Signup to reply.
  2. Are you certain you’re passing the post ID in correctly?
    get_the_ID() needs proper context, in this case, the WP Loop

    I’m assuming this is a function in your functions.php.

    // Gets global $post object
    global $post;
    
    $email = get_post_meta( $post->ID, 'women_email', true);
    $properties = $contact_form->get_properties();
    $properties['mail']['recipient'] = $email;
    $contact_form->set_properties($properties);
    
    return $contact_form; 
    
    

    This should do the trick.

    Login or Signup to reply.
  3. 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 that before_send_mail is an "ACTION" and not a "FILTER"

    Below is not tested, but should work. Put this in functions.php

    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 );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search