Search code examples
wordpressshortcodecontact-form-7

Wordpress Contact form 7 custom shortcodes


Contact form 7 has some shortcodes, like [_date] to get todays date. But I want to display the date one week from now.

So I need to create a custom shortcode to Contact form 7 that takes say [next_week] and in the recived email the correct date is displayed.

Where and how do I create custom shortcodes to Contact form 7?


Solution

  • Add the following to your functions.php

    wpcf7_add_shortcode('custom_date', 'wpcf7_custom_date_shortcode_handler', true);
    
    function wpcf7_custom_date_shortcode_handler($tag) {
        if (!is_array($tag)) return '';
    
        $name = $tag['name'];
        if (empty($name)) return '';
    
        $next_week = date('Y-m-d', time() + (60*60*24*7)); 
        $html = '<input type="hidden" name="' . $name . '" value="' . $next_week . '" />';
        return $html;
    }
    

    Now in the "Form" field in CF7 GUI type [custom_date next_week]

    Now you can use [next_week] in the message body.