Support » Plugin: Hidden Field for Contact Form 7 » Setting up filter question

  • Ok, here is the setup I want to reach.

    I have a contact page. I am using Contact Form 7. The subject field is a drop-down menu. We have 4 options.

    Selection_A means the email goes to email_1 and email_2
    Selection_B means the email goes to email_3 and email_4
    Selection_C means the email goes to email_5
    Selection_D means the email goes to email_5 and email_6

    From what I have been able to figure out, this is going to take (3) plugins to make happen. I’m OK with that.

    #1 – Contact Form 7 => That is the plugin for the form itself.
    #2 – Conditional Fields for Contact Form 7 => That is the plugin that gives me the ability to set “what if” settings.
    #3 – Hidden Field for Contact Form 7 => This lets me do hidden work to swap tags/fields around

    So, what I have figured so far, is my code needs to start off here:

    <label> 
        [text* your-name placeholder 'Your Name *' ] </label>
    
    <label>
        [email* your-email placeholder 'Your Email *'] </label>
    
    <label> 
        [select* subject "Becoming an athletic competitor" "Sponsoring the Highland Games" "Wanting to be a Vendor" "Clan Information" "Something not listed here"]</label>
    
    <label>
        [textarea* your-message placeholder 'Your Message *'] </label>
    
    [group mail-to-A]
    [text mail-to "email-1@domain.com"]
    [/group]
    
    [group mail-to-B]
    [text mail-to "email-2@domain.com"]
    [/group]
    
    [group mail-to-C]
    [text mail-to "email-3@domain.com"]
    [/group]
    
    [group mail-to-D]
    [text mail-to "email-4@domain.com"]
    [/group]
    
    [group mail-to-E]
    [text mail-to "email-5@domain.com"]
    [/group]
    
    [submit "Send"]

    Not so worried about the (2) emails yet. Want to get them changing first.

    Under the “Conditional Fields” tab, I have the rules set so that when the subject changes, the different groups get activated. Visually this works. I made the [mail-to-?] group a visible text entry spot and when I changed the subject, the display changed.

    Now I believe I am at the point where I need to make a filter to change the value of [mail-to] depending on what group is called. There is where I am stuck, and I believe this plugin (#3 – Hidden Field for Contact Form 7) is the trick. I hope I am right.

    I can see where I need to put the filter. But my question is, do I need a filter for each possible outcome like below?

    add_filter('mail-to-A', 'mail_to_select_A', 10, 2);
    function mail_to_select_A($value, $args) {
      // set value and return
      $value = 'email-1@domain.com';
      return $value;
    }
    
    add_filter('mail-to-B', 'mail_to_select_B', 10, 2);
    function mail_to_select_B($value, $args) {
      // set value and return
      $value = 'email-2@domain.com';
      return $value;
    }

    And the next question, would the code be:

    [group mail-to-A]
    [dynamichidden2 mail-to-A "mail_to_select_A"]
    [/group]
    
    [group mail-to-B]
    [dynamichidden2 mail-to-B "mail_to_select_B"]
    [/group]
    • This topic was modified 4 years, 5 months ago by .
Viewing 8 replies - 1 through 8 (of 8 total)
  • Thread Starter vader7071

    (@vader7071)

    Ok, tried to go back and edit. Just about everything I have above is incorrect.

    Here is what I have now, after a little more digging and understanding.

    In my “Contact Form 7” code:

    <label> 
        [text* your-name placeholder 'Your Name *' ] </label>
    
    <label>
        [email* your-email placeholder 'Your Email *'] </label>
    
    <label> 
        [select* subject "Becoming an athletic competitor" "Sponsoring the Highland Games" "Wanting to be a Vendor" "Clan Information" "Something not listed here"]</label>
    
    <label>
        [textarea* your-message placeholder 'Your Message *'] </label>
    
    [simplehidden dynamic-mail-to-filter "mail-to"]
    [simplehidden dynamic-mail-to-fields "subject"]
    
    [submit "Send"]

    FYI, [mail-to] is the tag I am trying to have change.

    I have added this snipped of code into my function.php file for the theme:

    /**
     * Dynamic Mail-To fields for Contact Form
     */
    /*  Select Mail  */
    
    function __construct() {
    	add_filter('wpcf7-dynamic-mail-to-example-filter', array($this, 'filter'), 10, 2);
    } // end public function __construct
    
    function filter($recipient, $args=array()) {
    	if (isset($args['subject'])) {
    		if ($args['subject'] == 'Becoming an athletic competitor') {
          		$recipient = 'email-1@domain.com';
        	} elseif ($args['subject'] == 'Sponsoring the Highland Games') {
          		$recipient = 'email-2@domain.com';
        	} elseif ($args['subject'] == 'Wanting to be a Vendor') {
          		$recipient = 'email-3@domain.com';
        	} elseif ($args['subject'] == 'Clan Information') {
          		$recipient = 'email-4@domain.com';
        	} elseif ($args['subject'] == 'Something not listed here') {
          		$recipient = 'email-5@domain.com';
        	}
    	}
    	return $recipient;
    }

    When I run the form from my website, this is the body that is transmitted:

    From: [your-name] <[your-email]>
    Subject: [subject]
    
    [mail-to]
    
    Message Body:
    [your-message]
    
    -- 
    This e-mail was sent from a contact form on domain.com

    The [mail-to] is just to see if it is changing. When I test, the [mail-to] does not change. It remains exactly [mail-to]. It doesn’t even populate from the filter.

    Plugin Author John Huebner

    (@hube2)

    I’m really not sure after reading this what it is you’re attempting to do.

    But going back to your first post and looking at the first field

    
    [group mail-to-A]
    [dynamichidden2 mail-to-A "mail_to_select_A"]
    [/group]
    

    the hook you added was this

    
    add_filter('mail-to-A', 'mail_to_select_A', 10, 2);
    

    The hook is mail-to-A, mail_to_select_A is the name of the function. You field should include the hook name, not the function name.

    
    [dynamichidden2 mail-to-A "mail-to-A"]
    
    Thread Starter vader7071

    (@vader7071)

    Hi John. Yeah, I pretty much had myself totally confused in the beginning. Let’s see if I can start over and make it simple.

    I found your plugin (Dynamic Recipient for Contact Form 7). I believe this will do what I want to do. I found it after I started this thread. So if I need to move this, I don’t mind.

    That being said. I have a drop down list for my tag called [subject]. The user can choose one of 5 choices.

    What I am trying to accomplish, is to change the value of a tag [mail-to] (this can be called anything) based on what is in [subject].

    Looking at Dynamic Recipient for Contact Form 7, it looks like that filter does just that. However, I am not getting the output. When the email sends, the tag [mail-to] remains exactly that, “[mail-to]”. It does not update.

    For testing purposes, I have the email going to a static email address and just a tag in the body that changes so I can see what is going on.

    In my second post, the upper block of code is what I have put into my function.php file. I constructed that from looking at the plugin examples.

    Plugin Author John Huebner

    (@hube2)

    Actually, that’s where I thought this was in the beginning. After your first comment when I clicked the link in the email WP told me the page could not be found. After the second I hunted it down.

    I originally built the three CF7 plugins I have to work together. The dynamic select field to allow a dynamic select field for “Subject”, the dynamic recipient plugin to send emails based on the subject selected and the hidden field to allow me to specify the hook of for the recipient field and allow supplying additional argument.

    Sorry about the documentation being confusing. They are all relatively simple plugins, but can be extremely powerful used in concert. The explanations only outline the simplest of uses.

    Thread Starter vader7071

    (@vader7071)

    Right now I have Dynamic Recipient for Contact Form 7 and Hidden Field for Contact Form 7 loaded. I am loading Dynamic Select now.

    Am I headed in the right direction?

    Am I correct putting the code in my function.php file?

    • This reply was modified 4 years, 5 months ago by .
    Thread Starter vader7071

    (@vader7071)

    Trying to get this figured out, let me share what all I have right now.

    1) I have the “Dynamic Recipient for Contact Form 7” plugin added. I also have the “Hidden Field” as well.

    In /[root]/wp-content/plugins/contact-form-7-dynamic-mail-to I have these files:
    cf7-dynamic-mail-to.php
    cf7-dynamic-mail-to-examples.php
    README.md
    readme.txt

    I have gone into examples and copied the code into notepad and modified it to what I need. I then used the example code and replace the code in just “cf7-dynamic-mail-to.php”. This is what I have:

    <?php 
    	/*
    		Plugin Name: Dynamic Recipient for Contact Form 7
    		Plugin URI: https://github.com/Hube2/contact-form-7-dynamic-mail-to
    		Description: Alter the to email address of mail in Contact Form 7 dynamically. Requires Contact Form 7
    		Version: 1.0.0
    		Author: John A. Huebner II
    		Author URI: https://github.com/Hube2/
    		License: GPL
    	*/
    
    	// If this file is called directly, abort.
    	if (!defined('WPINC')) { die; }
    	
    	//include(dirname(__FILE__).'/cf7-dynamic-mail-to-examples.php');
    	
    	new wpcf7_dynamic_mail_to();
    	
    	class wpcf7_dynamic_mail_to {
    		
    		// these are the email addresses to be used to for setting the recipient email address in cf7
    		private $email_address_1 = 'email-1@domain.com';
    		private $email_address_2 = 'email-2@domain.com';
    		private $email_address_3 = 'email-3@domain.com';
    		private $email_address_4 = 'email-4@domain.com';
    		private $email_address_5 = 'email-5@domain.com';
    		
    		public function __construct() {
    			add_filter('wpcf7-dynamic-mail-to-filter', array($this, 'filter'), 10, 2);
    		} // end public function __construct
    		
    		public function filter($recipient, $args=array()) {
    			//echo '(',$recipient,')';
    			//print_r($args); die;
    			if (isset($args['subject'])) {
    				if ($args['subject'] == 'Becoming an athletic competitor') {
    					$recipient = $this->email_address_1;
    				} elseif ($args['subject'] == 'Sponsoring the Highland Games') {
    					$recipient = $this->email_address_2;
    				} elseif ($args['subject'] == 'Wanting to be a Vendor') {
    					$recipient = $this->email_address_3;
    				} elseif ($args['subject'] == 'Clan Information') {
    					$recipient = $this->email_address_4;
    				} elseif ($args['subject'] == 'Something not listed here') {
    					$recipient = $this->email_address_5;
    				}
    			}
    			return $recipient;
    		} // end public function filter
    		
    	} // end class wpcf7_dynamic_mail_to
    	
    ?>

    In my Contact Form 7 form, I have this code:

    <label> 
        [text* your-name placeholder 'Your Name *' ] </label>
    
    <label>
        [email* your-email placeholder 'Your Email *'] </label>
    
    <label> 
        [select* subject "Becoming an athletic competitor" "Sponsoring the Highland Games" "Wanting to be a Vendor" "Clan Information" "Something not listed here"]</label>
    
    <label>
        [textarea* your-message placeholder 'Your Message *'] </label>
    
    [simplehidden wpcf7-dynamic-mail-to-filter "mail-to"]
    [simplehidden wpcf7-dynamic-mail-to-filter "subject"]
    
    [submit "Send"]

    Then in my “Mail” settings, in the body of the email, I have a tag [mail-to] just to make sure it is changing.

    Everything looks like it should work, but it isn’t. The PHP file that the code is in is called “cf7-dynamic-mail-to.php” while the filter being called is “wpcf7-dynamic-mail-to”. Could that be the issue? I am at a total loss of what I have wrong or what I have missing.

    • This reply was modified 4 years, 5 months ago by .
    Thread Starter vader7071

    (@vader7071)

    An update. In my CF7 Form tab, I have these 2 lines:

    [simplehidden wpcf7-dynamic-mail-to-filter "subject"]
    [simplehidden wpcf7-dynamic-mail-to-filter "mail-to"]

    In my CF7 Mail tab, I have these 2 lines:

    [mail-to]
    [wpcf7-dynamic-mail-to-filter]

    In the plugin folder (contact-form-7-dynamic-mail-to) I have a file named

    cf7-dynamic-mail-to.php

    that has the following code. This same code is also in my

    function.php

    file for the theme.

    new wpcf7_dynamic_email_to();
    	
    	class wpcf7_dynamic_email_to {
    		
    		// these are the email addresses to be used to for setting the recipient email address in cf7
    		private $email_address_1 = 'email-1@domain.com';
    		private $email_address_2 = 'email-2@domain.com';
    		private $email_address_3 = 'email-3@domain.com';
    		private $email_address_4 = 'email-4@domain.com';
    		private $email_address_5 = 'email-5@domain.com';
    		
    		public function __construct() {
    			add_filter('wpcf7-dynamic-email-to', array($this, 'filter'), 10, 2);
    		} // end public function __construct
    		
    		public function filter($recipient, $args=array()) {
    			//echo '(',$recipient,')';
    			//print_r($args); die;
    			if (isset($args['subject'])) {
    				if ($args['subject'] == 'Becoming an athletic competitor') {
    					$recipient = $this->email_address_1;
    				} elseif ($args['subject'] == 'Sponsoring the Highland Games') {
    					$recipient = $this->email_address_2;
    				} elseif ($args['subject'] == 'Wanting to be a Vendor') {
    					$recipient = $this->email_address_3;
    				} elseif ($args['subject'] == 'Clan Information') {
    					$recipient = $this->email_address_4;
    				} elseif ($args['subject'] == 'Something not listed here') {
    					$recipient = $this->email_address_5;
    				}
    			}
    			return $recipient;
    		} // end public function filter
    		
    	} // end class wpcf7_dynamic_mail_to

    Here are my results.

    When I send the email, this is what I get:

    [mail-to]
    mail-to

    So the [mail-to] tag is not getting filled and the form is just dumping the place holder as is. But the tag [wpcf7-dynamic-mail-to-filter] is getting filled with “mail-to”.

    So, here is where I am. I have made a little progress, but I am still at a loss. I cannot seem to find why the tags are not being filled properly.

    • This reply was modified 4 years, 5 months ago by .
    Thread Starter vader7071

    (@vader7071)

    John, have you had a chance to look at my updated information?

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Setting up filter question’ is closed to new replies.