13

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?

6 Answers 6

21

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.

4
  • 2
    I use a simpler version for my needs: wpcf7_add_shortcode('input_name', function( $tag ) { return '<input name="'.$tag['basetype'].'" value="your_value" />' });
    – vladkras
    Sep 9, 2015 at 18:35
  • 1
    wpcf7_add_shortcode() is deprecated, use wpcf7_add_form_tag() instead (although you will be warned if you have debug on).
    – Chris Pink
    Mar 11, 2021 at 12:58
  • @ChrisPink, wpcf7_add_form_tag() is not working, it just prints the short code as it is.
    – divy3993
    Feb 17 at 13:40
  • @divy3993 I believe this is a bug in 5.7.3
    – Chris Pink
    Feb 20 at 7:43
2

This is a bit late to the response party, but I keep seeing this post when I want to add custom shortcodes to my forms and message body. I wanted to be able to insert shortcodes without registering them special in CF7 and often in the message body only (something CF7 doesn't seem to be able to do).

Here's how I finally did it:

// Allow custom shortcodes in CF7 HTML form
add_filter( 'wpcf7_form_elements', 'dacrosby_do_shortcodes_wpcf7_form' );
function dacrosby_do_shortcodes_wpcf7_form( $form ) {
    $form = do_shortcode( $form );
    return $form;
}

// Allow custom shortcodes in CF7 mailed message body
add_filter( 'wpcf7_mail_components', 'dacrosby_do_shortcodes_wpcf7_mail_body', 10, 2 );
function dacrosby_do_shortcodes_wpcf7_mail_body( $components, $number ) {
    $components['body'] = do_shortcode( $components['body'] );
    return $components;
};

// Add shortcode normally as per WordPress API
add_shortcode('my_code', 'my_code_callback');
function my_code_callback($atts){
    extract(shortcode_atts(array(
        'foo' => 'bar'
    ), $atts));

    // do things
    return $foo;
}
2

There are two types of tags in CF7: form tags (the contact form itself) and mail tags (a email) — read more.


  1. Custom form tags:

    To add a custom form tag you can use wpcf7_add_form_tag() function on wpcf7_init action hook (Read more).

    The wpcf7_add_shortcode() function in the accepted answer is considered to be deprecated and replaced by this function.

  2. Custom mail tags:

    I didn't find any built-in functionality to add custom mail tags but I believe there are multiple possible workarounds here:

    1. Enable custom shortcodes (don't forget to create the shortcode handler beforehand):

      • For mail components, according to the DACrosby's answer.
      • For entire mail (all components), using wpcf7_special_mail_tags filter:

    function my_special_mail_tag( $output, $name, $html ) {
        if ( 'myshortcode' === $name ) {
            $output = do_shortcode( "[$name]" );
        }
        return $output;
    }
    add_filter( 'wpcf7_special_mail_tags', 'my_special_mail_tag', 10, 3 );
    
    1. Add a custom hidden form tag with the prepopulated data to the form, and then use it in the mail: [my-custom-form-tag-with-some-prepopulated-data]; the custom form tag should be registered with the wpcf7_add_form_tag(), as specified above.
0

My solution is simple. Do whatever shortcode you want and place it on contact form area. Just don't forget do the following: Place only the following code in functions.php and your shortcode will work:

add_filter( 'wpcf7_form_elements', 'do_shortcode' );

0

Enable shortcodes inside the Form Template

Solution 1 - using wpcf7_form_elements filter

With the wpcf7_form_elements filter, we can modify the HTML output of the form. Since the output will contain shortcodes that were added in the Form Template, all we need to do is bind do_shortcode function as a callback to this filter inside functions.php, like so:

add_filter( 'wpcf7_form_elements', 'do_shortcode' );

Solution 2 - using Contact Form 7 Extension Plugin

If you are uncomfortable with editing functions.php file, there is a plugin available Contact Form 7 Shortcode Enabler that does exactly the same thing.

Note: Currently, the plugin latest update was 5 years ago but the plugin only executes the same line of code as in solution 1, so it should still work with the latest releases of WordPress.

Enable shortcodes inside the Mail Template

The wpcf7_special_mail_tags filter is used to customize the output of form-tags and special tags of Contact Form 7. Luckily, this filter also detects the custom shortcodes that are found in the Mail Template as well, so we can use this filter to process those shortcodes too.

You need to add the following code inside the functions.php file:

function my_special_mail_tag( $output, $name, $html ) {
    if ( 'myshortcode' == $name )
        $output = do_shortcode( "[$name]" );
 
    return $output;
}
add_filter( 'wpcf7_special_mail_tags', 'my_special_mail_tag', 10, 3 );
-3

I haven't do before, but I think that shortcodes are managed by wordpress itself (even for plugins as CF7).

An example to create a simple shortcode is:

//[foobar]
function foobar_func( $atts ){
 return "foo and bar";
}
add_shortcode( 'foobar', 'foobar_func' );

Placed in functions.php.

For more information: http://codex.wordpress.org/Shortcode_API

Or you can use a plugin like this that do the work: http://wordpress.org/extend/plugins/shortbus/

3
  • 1
    CF7 prints [foobar], posts and pages print "foo and bar". So it did not work.
    – halliewuud
    Nov 2, 2012 at 8:02
  • This works fine when you would like to use the shortcode in the form but it does not work in the email beeing sent. Se my answer for the correct solution
    – halliewuud
    Nov 2, 2012 at 9:51
  • This answer shows how to create a custom shortcode. Unfortunately, that was not the question being asked. The question was how to get a custom shortcode to be parsed in an email.
    – butlerblog
    Mar 28, 2019 at 0:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.