CaTourist
CaTourist

Reputation: 733

Contact Form 7 and cookie value

Good morning, I'm using Contact Form 7 with email submission after form submission. Now i need to send by email a value that is stored into a cookie (it is a referral code). How can i do it?

Upvotes: 2

Views: 6766

Answers (4)

Reza Mamun
Reza Mamun

Reputation: 6189

Without any plugin, it is very simple to achieve the same just adding a new custom Special Mail Tag:

STEP-1: Add below code in your theme's functions.php

add_filter( 'wpcf7_special_mail_tags', 'wpcf7_my_cookie_mailtag', 10, 3 );
function wpcf7_my_cookie_mailtag( $output, $name, $html ) {
    if ( '_my_cookie_special_tag' != $name ) { // rename the tag name as your wish;
        return $output;
    }

    if ( ! $contact_form = WPCF7_ContactForm::get_current() ) {
        return $output;
    }

    $val = isset($_COOKIE['my_cookie']) ? $_COOKIE['my_cookie'] : '';

    return $html ? esc_html($val) : $val;
}

STEP-2: Use this shortcode [_my_cookie_special_tag] in your Mail Setup.

Upvotes: 5

wdonayredroid
wdonayredroid

Reputation: 431

I think what you are trying to do is to send another email after a successful contact form 7 submission. You may do this through the available wpcf7 hook in the backend. wpcf7_mail_sent

Note: You have to get the cookie data you want and include it on the form dynamically as a hidden field using javascript so that its included in the form submitted which you can retrieve at the backend.

//you can place this in your functions.php
add_action('wpcf7_mail_sent', function ($cf7) {
  //do what you want here like get the extra  
});

This is how I would do it in the backend end assuming that I already have the data from a cookie added as a hidden field using javascript/jquery e.g. injected <input type="hidden" name="referral_code" /> inside form tag.

add_action('wpcf7_mail_sent', function ($cf7) {

    $wpcf7         = WPCF7_ContactForm::get_current();
    $submission    = WPCF7_Submission::get_instance();
    $posted_data   = empty($submission) ? null : $submission>get_posted_data();

    //assuming you are tracking a form with an id 1234
    if($wpcf7->id() === 1234){

      //not sure if this still works, if not you can simply use $_GET['referral_code']
      if(isset($posted_data['referral_code'])){
         $referralCode = $posted_data['referral_code'];
         //...now from this point you can send an email or pass this info to another platform for tracking purposes.
      }

    }

});

Upvotes: 0

Outsource WordPress
Outsource WordPress

Reputation: 3799

Here are the steps to achieve this.

  1. Install Contact Form 7 Dynamic Text Extension plugin.

  2. Add this to your theme's 'functions.php'.

    function dynamictext_cf7_cookie($atts){
        extract(shortcode_atts(array(
            'key' => -1,
        ), $atts));
    
        if($key == -1) return '';
        $val = '';
    
        if( isset( $_COOKIE[$key] ) ){
            $val = $_COOKIE[$key];
        }
    
        return $val;
    }
    add_shortcode('DT_CF7_COOKIE', 'dynamictext_cf7_cookie');
    
  3. Add this to your Contact Form 7 form's 'Form' tab - [dynamichidden referral-code-field "DT_CF7_COOKIE key='REFERRAL_CODE'"] where 'REFERRAL_CODE' is your PHP cookie name.

  4. Add this to the 'Mail' tab of your Contact Form 7 form - [referral-code-field].

That's it and you can read more here - https://www.sean-barton.co.uk/2014/04/contact-form-7-place-post-server-cookie-session-variables-fields/.

Upvotes: 2

charan kumar
charan kumar

Reputation: 2157

You can check the documentation, as per docs, contact form7 wont use cookies.

With the default configuration, this plugin, in itself, does not:

  1. track users by stealth;
  2. write any user personal data to the database;
  3. send any data to external servers;
  4. use cookies.

https://wordpress.org/plugins/contact-form-7/

Upvotes: 0

Related Questions