How to

Enable shortcode in Contact Form 7 Form & Mail Template in WordPress

In one of my previous posts, it was demonstrated how to enable shortcode in widgets, excerpts & custom fields. Another example where we might need the shortcode to work is with the Contact Form 7 plugin. We might want to add them in the Mail Template, or we might want to use them in the Form Template to style the form to our liking.

Enable shortcodes inside the Form Template

In the Form Template section, we customize our form. The shortcodes that we could use here are those for styles, either custom ones or the ones already provided by the theme or third-party plugins.

Contact Form 7 - Form Template using Shortcode

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.

Enable shortcodes inside the Mail Template

The Mail Template is where we customize the mail that will be submitted. Here, we can use the tags that were defined in the Form Template.

Contact Form 7 - Mail Template

The Contact Form 7 also gives us a list of special mail tags that we can use. The [_time] for example will display the time of the submission. For a full list of these special tags, check this page.

Solution – using wpcf7_special_mail_tags filter

We can use the wpcf7_special_mail_tags filter to customize the output of these special tags. Since this filter also detects the custom shortcodes that are found in the Mail Template as well, we can use it to process those shortcodes.

We 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 );
Note: Currently, only the self-enclosed shortodes will work with wpcf7_special_mail_tags filter, while the enclosing shortcodes of format [myshortcode]...[/myshortcode] and the shortcodes with the attributes [myshortcode foo="bar"] will not.

Line by line code explanation

  • Line 1:

    The wpcf7_special_mail_tags filter has three parameters:

    • $output
      With the special mail tag, the $output will contain the output of that tag. For example, [_time] would have the time of the submission. With the custom shortcode, the $output will be empty.
    • $name
      The $name contains the name of the tag. With the custom shortcode, it will have the name of the shortcode.
    • $html
      The $html has a value of 0 or 1, depending on the “Use HTML content type” checkbox inside the Mail Template.
  • Line 2-3:

    Here, we check if the name tag is our custom shortcode, if so, we call do_shortcode function and store the result in $output variable.

  • Line 5:

    Finally, we return the $output variable.


Add a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.