Viewing 2 replies - 1 through 2 (of 2 total)
  • Looks like you are looking for Conditional Logic which is not available directly in the Contact Form 7 plugin.

    It can be done via custom programing but you would need to be rather skilled at WordPress development using PHP and/or jQuery.

    Some of the premium Form plugins do offer some Conditional Logic including Gravity Forms and Ninja Forms.

    Thanks for your reply. I found this Plugin: Contact Form 7 Mail Conditions. Unfortunately it is not working and I am not into plugin developing, but it seems to be a good approach. Maybe somebody sees the error, why it is not working?

    define('WPCF7MC_EXPRESSION', 0);
    define('WPCF7MC_VARIABLE', 1);
    define('WPCF7MC_VALUE', 2);
    
    class Contact_Form_7_Mail_Conditions {
    
    	public function __construct($regexp = '/\[if\s+(\S+?)\](.*?)\[\/if\]/') {
    		$this->init_action();
    		$this->regexp = $regexp;
    	}
    
    	public function before_send_mail(&$cf7) {
    		$mail_body = $cf7->mail['body'];
    		$cf7->mail['body'] = $this->process_conditions($cf7, $cf7->mail['body']);
    		$cf7->mail_2['body'] = $this->process_conditions($cf7, $cf7->mail_2['body']);
    	}
    
    	private function process_conditions(&$cf7, $mail_body) {
    		$updated_email_body = $mail_body;
    		$matches = array();
    		$num_matches = preg_match_all($this->regexp, $mail_body, $matches);
    		for ($i=0; $i < $num_matches; $i++) {
    			$expression = $matches[WPCF7MC_EXPRESSION][$i];
    			$variable = $matches[WPCF7MC_VARIABLE][$i];
    			$value = $matches[WPCF7MC_VALUE][$i];
    			if (empty($cf7->posted_data[$variable]) and array_key_exists($variable, $cf7->posted_data)) {
    				$updated_email_body = str_replace($expression, '', $updated_email_body);
    			} else {
    				$updated_email_body = str_replace($expression, $value, $updated_email_body);
    			}
    		}
    		$updated_email_body .= ' lorem ipsum';
    		return $updated_email_body;
    	}
    
    	private function init_action() {
    		if (function_exists('add_action'))
    			add_action("wpcf7_before_send_mail", array($this, 'before_send_mail'));
    	}
    } 
    
    new Contact_Form_7_Mail_Conditions;

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘If statement in mails’ is closed to new replies.