Contact Form 7 – place GET, POST, SERVER, COOKIE, SESSION etc variables into fields

April 16th, 2014 by Sean Barton Leave a reply »

As part of a job I am doing to extend a Contact Form 7 implementation and to pay additional homage to the plugin itself I would like to show you how to get PHP environment variables into your fields. Until now I have found myself writing complex and frankly undocumentable (through a combination of lack of time and shame at my hacky approach!) snippets to get custom data into my clients’ contact forms.

I had a similar job today whereby I had to get a $_COOKIE value into a hidden input field and also a custom PHP generated string which isn’t possible out of the box with Contact Form 7. I found the ‘Contact Form 7 Dynamic Text Extension‘ plugin after a bit of searching and although it didn’t do exactly what I wanted I sussed that it supports shortcodes. So rather than having to code a custom CF7 solution of my own I simply had to write a couple of shortcode handlers.. simples!

Out of the box you can to GET and POST with a few others. I wanted COOKIE and another of my own. The following code you can put into your theme functions.php file and in conjunction with the code for CF7 below that to generate the hidden field you can have any data prefilled into a CF7 form in minutes.

Example – Cookie

This is more or less a carbon copy of the plugin author’s own GET/POST code. I would have used a slightly more compact approach but it didn’t make sense to be different for the sake of it for the sake of my vanity. This is the code for your functions.php file:

function sb_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('SB_CF7_COOKIE', 'sb_cf7_cookie');

The code to add this to your CF7 form is as follows:

[dynamichidden field-name "SB_CF7_COOKIE key='COOKIE_NAME'"]

Where field-name is the name of the field (go figure!) which you would use in the notification email and user email if necessary like this: [field-name]. The COOKIE_NAME part is referencing $_COOKIE['COOKIE_NAME'] or, for clarity, just a cookie called COOKIE_NAME.

Example 2 – PHP Logic

I won’t explain this one so much but essentially the shortcode can do whatever you need it to.. You don’t even need to pass the shortcode anything if you are going to rely on the page environment. The following is an extract taken from a contact form which was ONLY ever used on WP taxonomy pages for WooCommerce (product categories) and is designed to return the category name in a field:

function sb_cf7_catname(){
    $cat_id = get_queried_object_id(); //get category id
    $cat = get_term($cat_id, 'product_cat');  //get category object
    $val = $cat->name; //get category name

    return $val;
}
add_shortcode('SB_CF7_CATNAME', 'sb_cf7_catname');

Notice this time we don’t look for a shortcode argument in $atts and use the page object ID to work from. Because we require no shortcode arguments then the CF7 declaration is simpler as follows:

[dynamictext field-name "SB_CF7_CATNAME"]

Hopefully you will find it useful. Do let me know if you want to write a custom shortcode for CF7 (or even just a custom shortcode) and can’t get it right. I am always willing to help…

4 comments

  1. Yvonne says:

    I add a contact form in woocommerce, single-product page, is it possible to show product name automatically?

  2. Yvonne says:

    What I write: but not work

    function cf7_product_title(){

    $product_name = get_the_title();
    $val = $product_name;

    return $val;
    }
    add_shortcode(‘CF7_PRODUCT_TITLE’, ‘cf7_product_title’);

    And insert,
    [dynamictext* dynamictext-343 "cf7_product_title"]

    • Sean Barton says:

      No problem Yvonne, you have the right idea. WP Shortcodes are case sensitive I believe so the CF7 declaration would be CF7_PRODUCT_TITLE not cf7_product_title

      Otherwise you could try the following instead of get_the_title();

      global $post;
      $val = $post->post_title;

      ta
      S

Leave a Reply