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){
        '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…

A donate button!

Be the first to hear about new products/updates!

This is a mailing list for those people interested in being told when we release a new product (Divi plugin or Theme).

We shall also use this list to let you know about product updates and releases.

You have Successfully Subscribed!