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

Dynamic Attributes

What are form field attributes?

Attributes help define the appearance and behavior of form fields, such as restricting users to a specific number range with minimum and maximum attributes. The most common attributes are id and class. Most input elements should have a valid type attribute. Not all attributes are valid on all elements. For example, the placeholder attribute is not a valid attribute on select elements, but is perfectly valid on input elements with a type attribute set to text.

Configurable Attributes and Boolean Attributes

Some attributes are configurable with different options while others simply need to exist. An example of a configurable attribute is like the id or class. In order for those to be valid, the HTML needs those attributes to be set to a valid value like so:

<input id="dynamic-input" class="dtx" />

A Boolean attribute does not need a value, it simply needs to exist. An example of a Boolean attribute is like required or readonly. You may also see these as set to themselves like so:

<input readonly />
<input readonly="readonly" />

Contact Form 7 Attributes

In Contact Form 7, attributes are defined in the form tag by attribute, followed by a colon (:) and a value. Boolean attributes have just the attribute’s name. For example, a form tag that is not allowed to be edited by the user would need the readonly attribute like this:

[text static_text readonly "Hello world"]

In another example, setting the maximum length of a required text field to 50 characters would be achieved with this form tag:

[text* static_text maxlength:50]

In Contact Form 7, the follow attributes are available:

View more attributes available specific to text fields, number fields, date fields, and checkboxes/radio buttons/menus.

Dynamic Attributes in DTX

In version 4.4.0 of Contact Form 7 – Dynamic Text Extension, we introduced dynamic attributes!

In the form tag generator for the dynamic text field, you’ll see that there are text input fields for some of the form tags (where they’re valid HTML of course!). For example, you’ll see options to set the Range for number, range, and date fields. On others, you’ll see new options to set values for Auto-Complete and Auto-Capitalize. These values can be static or accept a shortcode value that outputs a valid string.

For Boolean attributes, shortcodes that return an empty string are considered valid, so to dynamically remove that attribute, pass something other than the attribute’s name or a blank string!

For example, let’s say a field named hello is set to only be editable if the URL contains the parameter foo. We can achieve that by using our CF7_GET shortcode setting the key to foo (because that is the parameter’s name in the URL) and defining the default value to be bar which is what we want the shortcode to return if foo does not exist or is empty. So that shortcode configuration looks like this:

CF7_GET key='foo' default='bar'

To work with Contact Form 7’s form tag syntax, the value you type into the text field undergoes two changes: it converts apostrophes to their HTML entity code and encodes the whole thing like a URL. This is what will allow you to use apostrophes in static text or shortcodes with attributes for dynamic content. Though the output looks less-than friendly. In our example, the above gets turned into this:

CF7_GET%20key%3D%26%2339%3Bfoo%26%2339%3B%20default%3D%26%2339%3Bbar%26%2339%3B

And then inserted as the attribute’s value for readonly like this:

[dynamic_text hello readonly:CF7_GET%20key%3D%26%2339%3Bfoo%26%2339%3B%20default%3D%26%2339%3Bbar%26%2339%3B "Some Static Value"]

If viewing the URL where ?foo or ?foo=anything or ? (because there is no foo!), the dynamic field will be editable because missing/blank values return bar by default, and anything other than readonly (the attribute’s own name) is invalid for Boolean attributes. However, if the URL includes ?foo=readonly, then the field will not be editable because the shortcode will return the attribute’s own name, making it valid.

The following attributes accept dynamic values:

  • autocapitalize – great for accessibility, the form tag generator includes suggestions for common options, see documentation for the auto-capitalize attribute.
  • autocomplete – great for accessibility, the form tag generator includes suggestions for common options, see documentation for the auto-complete attribute.
  • autofocus – a Boolean attribute, great for accessibility, it sets the focus on the form field when the page is loaded, see documentation for the auto-focus attribute.
  • cols – only valid on textarea elements, this defines the number of columns. See documentation for the cols attribute.
  • disabled – a Boolean attribute, prevents this form field from being submitted, see documentation for the disabled attribute.
  • list – allows text-based fields to offer suggestions, see documentation for the data list element and the associated list attribute.
  • max – a ranged attribute, restricts a numeric or date field to be less than this value, see documentation for the max attribute.
  • maxlength – a ranged attribute, restricts a text-based field to be less than the number of characters, see documentation for the maxlength attribute.
  • min – a ranged attribute, restricts a number or date field to be greater than this value, see documentation for the min attribute.
  • minlength – a ranged attribute, restricts a text-based field to be greather than the number of characters, see documentation for the minlength attribute.
  • pattern – see documentation on the pattern attribute.
  • placeholder and watermark – See DTX Placeholder documentation
  • readonly – a Boolean attribute, prevents users from editing the input or textarea fields. See documentation for the read-only attribute.
  • rows – only valid on textarea elements, this defines the number of rows. See documentation for the rows attribute.
  • size – the size attribute of the input and select elements. Default is 40 for input elements and 1 for select elements, see documentation on the size attribute.
  • step – defines how a numeric field is incremented, see documentation on the step attribute.

For ranged attributes, if both values exist, the minimum must be smaller than or equal to the maximum.

There are probably some bugs with this feature, so if you find one, please report it in the support forums. Thank you!

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) ""
}