WordPress.org

Forums

Contact Form 7
using a variable as address to send the mail to (2 posts)

  1. raffi.vitis
    Member
    Posted 3 years ago #

    I run a wordpress site that features various circus artists. each page a contact information that is takes form the meta-data entered for each show. I would like to use the e-mail stored in the meta-data.

    $mail = get_post_meta( $post->ID, 'mail', true );

    I would like to use $mail as the recipient of the mail sent through the contact form 7 - different recipient for each page...

    How? Ideas?

    http://wordpress.org/extend/plugins/contact-form-7/

  2. Takayuki Miyoshi
    Member
    Plugin Author

    Posted 3 years ago #

    Add this code to your theme's functions.php:

    add_filter( 'wpcf7_special_mail_tags', 'my_wpcf7_special_mail_tag_for_mail_meta', 10, 2 );
    
    function my_wpcf7_special_mail_tag_for_mail_meta( $output, $name ) {
    
    	if ( ! isset( $_POST['_wpcf7_unit_tag'] ) || empty( $_POST['_wpcf7_unit_tag'] ) )
    		return $output;
    
    	if ( ! preg_match( '/^wpcf7-f(\d+)-p(\d+)-o(\d+)$/', $_POST['_wpcf7_unit_tag'], $matches ) )
    		return $output;
    
    	$post_id = (int) $matches[2];
    
    	if ( ! $post = get_post( $post_id ) )
    		return $output;
    
    	if ( '_my_mail_meta' == $name ) {
    		$mail = get_post_meta( $post->ID, 'mail', true );
    		$output = $mail;
    	}
    
    	return $output;
    }

    Then insert [_my_mail_meta] to the To field in the Mail section.

Topic Closed

This topic has been closed to new replies.

About this Plugin

  • Contact Form 7
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic