Shortcodes

USEFUL? 4

What is a shortcode?

A shortcode is a string of text that is processed and replaced by the output of a PHP function. WordPress shortcodes are denoted by square brackets surrounding the text string.

For example, you might have a shortcode called “name”. Then in a WordPress page, you might have this content:

Hello, [name], how are you?

When the page is served to the user, [name] is replaced by the output of the associated function, which should return the current user’s name. In this case, the output displayed to the user might be:

Hello, John, how are you?

So a shortcode is just a mapping of a string to a function that processes that string and some option attributes and returns a value.

How shortcodes are used in the Dynamic Text Extension

With a standard text field in Contact Form 7, you can set a default value. Here’s we’ve set the name field to “John”

With the DTX, we can use a shortcode for the default value in a Dynamic Text field

In this case, we’ve used the included shortcode, CF7_get_current_user, which returns the user name dynamically. However, we’ve eliminated the brackets, due to the syntax required for Contact Form 7. (The DTX acts as an intermediary to process the shortcode properly even without its brackets).

Shortcode syntax within the DTX

When using a shortcode as the Dynamic Value, you must:

  1. Remove the square brackets []
  2. Convert all double quotes " to single quotes '

These two changes are required to avoid syntax errors within Contact Form 7.

So, this shortcode:

[shortcode attribute="value"]

becomes

shortcode attribute='value'

when placed inside the dynamictext CF7 tag, e.g.:

[dynamictext name value="shortcode attribute='value'"]