Skip to content
Home » Documentation » Contact Form 7 – Dynamic Text Extension » Shortcodes » Custom Shortcodes

Custom Shortcodes

If you don’t have an existing shortcode to get the value you need, you can write a custom shortcode using PHP. All that is needed is defining the shortcode tag and the function that processes it to return the value.

Custom Shortcode Requirements:

  1. The shortcode must be self-closing. For example, [custom_shortcode] is valid while [custom_shortcode][/customshortcode] is not.
  2. All custom shortcodes must return a string type.
  3. It is highly recommended to sanitize and escape data. Feel free to apply the custom DTX filters like the built-in shortcodes do.

Create Custom Shortcodes to use in Contact Form 7 – Dynamic Text Extension

I might create the shortcode [au_display_copyright] to output a dynamic copyright year along with a company’s name specified as a shortcode attribute.

First, I’d start with an action that hooks into init and to add my custom shortcode tag [au_display_copyright]

function au_cf7dtx_copyright_init() {
    add_shortcode('au_display_copyright', 'au_cf7dtx_copyright_shortcode');
}
add_action('init', 'au_cf7dtx_copyright_init');

Then I’d write the callback function for my shortcode to ensure there’s always a default value for the company attribute (which is just blank) and to format it with the copyright character and current year. This makes it dynamic because this will automatically update every year to use the current year. The full string value is returned and that is what would be pre-filled in the contact form text field.

function au_cf7dtx_copyright_shortcode($atts = array(), $content = '', $tag = '') {
    // Normalize attribute keys to lowercase and configure default values
    $atts = shortcode_atts(array('company' => ''), array_change_key_case((array)$atts, CASE_LOWER), $tag);
    return sprintf('$copy; %s %s', date('Y'), $atts['company']);
}

Security Concerns

To keep your website safe, we highly recommend returning an escaped value. Escaping output is the process of securing output data by stripping out unwanted data, like malformed HTML or script tags. This process helps secure your data prior to rendering it for the end user. Learn more about the escaping functions provided by WordPress.

Alternatively, you can run your custom value through DTX’s escaping filter as you return it like this:

return apply_filters('wpcf7dtx_escape', $value);

You can pass up to four (4) parameters to the DTX escaping filter to modify how escaping is performed, including whether or not to obfuscate the value, which escaping method to use, and what protocols you want to allow for URL escaping.

Additional escaping examples with DTX

If $value is a multi-line text you’re using to prefill a textarea element and you do not want it obfuscated:

return apply_filters('wpcf7dtx_escape', $value, false, 'textarea');

If $value is a URL like https://example.com and you want it obfuscated:

return apply_filters('wpcf7dtx_escape', $value, true, 'url');

If $value is a URL but you also want to allow specific protocols like mailto, tel, and sms (see documentation on allowed protocols), while also obfuscating it.

return apply_filters('wpcf7dtx_escape', $value, true, 'url', array('http', 'https', 'mailto', 'tel', 'sms'));

Use Custom Shortcodes in Contact Form 7 – Dynamic Text Extension

Once you have your custom shortcode tag created, you can use it in your forms created in Contact Form 7 by setting the value of your dynamic form fields with it.

For this example, I’ll be using a Dynamic Text form tag. With the DTX shortcode syntax in mind, you’d leave out the square brackets and only use single quotes for any attributes you’re passing. Continuing with the shortcode above, the output of that field in the form’s edit template would look like this if I specified the “company” attribute as “AuRise Creative”:

[dynamic_text copyright "au_display_copyright company='AuRise Creative'"]

And when viewed on the frontend, the value of my text field would read, “© 2023 AuRise Creative” with the date dynamically updated to the current year.

Learn More about Custom Shortcodes

For more information about writing custom shortcodes for WordPress, check out the Shortcode API.

Related Post Module Attributes Before

array(29) {
  ["post_type"]=>
  string(4) "post"
  ["post_id"]=>
  string(1) "0"
  ["exclude"]=>
  string(1) "0"
  ["title"]=>
  string(32) "Related Articles & Tutorials"
  ["description"]=>
  string(0) ""
  ["max"]=>
  string(1) "3"
  ["post_ids"]=>
  string(0) ""
  ["exclude_ids"]=>
  string(0) ""
  ["is_series"]=>
  string(0) ""
  ["featured_term"]=>
  string(3) "307"
  ["exclude_terms"]=>
  string(0) ""
  ["exclusive"]=>
  string(1) "1"
  ["order"]=>
  string(4) "DESC"
  ["show_image"]=>
  string(2) "on"
  ["image_size"]=>
  string(6) "medium"
  ["menu_order_label"]=>
  string(0) ""
  ["show_order_label"]=>
  string(2) "on"
  ["show_date"]=>
  string(2) "on"
  ["show_meta_keys"]=>
  string(2) "on"
  ["show_modified"]=>
  string(0) ""
  ["show_author"]=>
  string(0) ""
  ["show_categories"]=>
  string(0) ""
  ["show_primary_category"]=>
  string(0) ""
  ["show_description"]=>
  string(0) ""
  ["show_reading_time"]=>
  string(2) "on"
  ["show_cta"]=>
  string(2) "on"
  ["cta"]=>
  string(9) "Read more"
  ["autoplay"]=>
  string(0) ""
  ["allow_sticky"]=>
  string(0) ""
}

Related Post Module Attributes

array(29) {
  ["post_type"]=>
  string(4) "post"
  ["post_id"]=>
  string(1) "0"
  ["exclude"]=>
  string(1) "0"
  ["title"]=>
  string(32) "Related Articles & Tutorials"
  ["description"]=>
  string(0) ""
  ["max"]=>
  string(1) "3"
  ["post_ids"]=>
  string(0) ""
  ["exclude_ids"]=>
  string(0) ""
  ["is_series"]=>
  string(0) ""
  ["featured_term"]=>
  string(3) "307"
  ["exclude_terms"]=>
  string(0) ""
  ["exclusive"]=>
  string(1) "1"
  ["order"]=>
  string(4) "DESC"
  ["show_image"]=>
  string(2) "on"
  ["image_size"]=>
  string(6) "medium"
  ["menu_order_label"]=>
  string(0) ""
  ["show_order_label"]=>
  string(2) "on"
  ["show_date"]=>
  string(2) "on"
  ["show_meta_keys"]=>
  string(2) "on"
  ["show_modified"]=>
  string(0) ""
  ["show_author"]=>
  string(0) ""
  ["show_categories"]=>
  string(0) ""
  ["show_primary_category"]=>
  string(0) ""
  ["show_description"]=>
  string(0) ""
  ["show_reading_time"]=>
  string(2) "on"
  ["show_cta"]=>
  string(2) "on"
  ["cta"]=>
  string(9) "Read more"
  ["autoplay"]=>
  string(0) ""
  ["allow_sticky"]=>
  string(0) ""
}

2 thoughts on “Custom Shortcodes”

  1. Great plugin!

    I wondering if there is a way to get the Post-Category-Name with “Contact Form 7 – Dynamic Text Extension” plugin??

    Could you add this feature in near future?

    Thanks a lot.