Documentation: Contact Form 7 - Dynamic Text Extension

Contact Form 7 – Dynamic Text Extension Knowledgebase 0

Overview The Dynamic Text Extension (DTX) is a very simple plugin that extends the functionality of Contact Form 7. Out of the box in Contact Form 7, you can set a static default value for text inputs. For example, if you have a field with a “name” input, you could set the default of that field to “John”, and the name field would then be pre-filled with the text “John” when the user visits the page. The DTX allows you to set this default dynamically, with the value generated by a shortcode. So a more useful example in the case above, we’d replace the static “John” with a shortcode that returns the logged in user’s name. Now the name input is pre-filled with the current user’s name when they view the form. The value is no longer static (“John”), but dynamic in that it changes based on the user viewing the form. In short, the DTX just allows you to set the text that appears by default in a text input via a shortcode. Requirements An input element of type ‘text’ or ‘hidden’. The plugin does not currently support textareas, radios, selects, or other form field types A shortcode that returns a value that can be rendered as text A value that can be determined server-side (as all shortcodes are processed server-side) What it doesn’t do The DTX does not: Work with fields other than text and hidden inputs Change field values based on user page interactions (you need Javascript for that) Provide a shortcode for every conceivable use case

View

Shortcodes 3

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: 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: 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: Remove the square brackets [] Convert all double quotes ” to single quotes ‘ These two changes are required to avoid syntax errors within Contact Form 7. So, this shortcode: becomes when placed inside the dynamictext CF7 tag, e.g.:

View

Included Shortcodes 0

The Dynamic Text Extension includes some shortcodes out of the box to make it easy to get started with the most common use cases. PHP GET Variables CF7_GET key=’foo’ This shortcode gives you access to the PHP GET variables, i.e. the arguments in the query string. Example To get the foo argument from the URL https://mysite.com?foo=bar You would use the shortcode CF7_GET key=’foo’ in a dynamic text tag: [dynamictext inputname “CF7_GET key=’foo'”] PHP POST Variables CF7_POST key=’foo’ Provides access to the PHP $_POST variables array by key. Example [dynamictext inputname “CF7_POST key=’foo'”] Blog/Site Info CF7_bloginfo show=’url’ Provides access to the basic site info from the get_bloginfo() function. Examples Dynamic Text tag: [dynamictext inputname “CF7_bloginfo show=’url'”] Shortcode variations: CF7_bloginfo show=’name’ Site Title CF7_bloginfo show=’url’ Site URL CF7_bloginfo show=’version’ WordPress version Post Info CF7_get_post_var key=’title’ Provides access to information about the current post/page that the form is displayed on based on the properties of the current global $post object. Example Dynamic Text tag: [dynamictext inputname “CF7_get_post_var key=’title'”] Shortcode variations: CF7_get_post_var key=’title’ Current post title CF7_get_post_var key=’slug’ Current post slug CF7_get_post_var key=’post_author’ Current post author’s ID Current URL CF7_URL Returns the current URL of the page the form is on. Shortcode does not have any arguments. Example Dynamic Text tag: [dynamictext inputname “CF7_URL”] Custom Fields CF7_get_custom_field key=’my_custom_field’ Access custom meta fields from the current post/page. Use the custom field’s ID as the key. Example Dynamic Text tag: [dynamictext inputname “CF7_get_custom_field key=’my_custom_field'”] Current User Info CF7_get_current_user key=’user_field’ Access current user’s info based on wp_get_current_user() If no key is provided, will default to user_login. Example Dynamic Text tag: [dynamictext inputname “CF7_get_current_user key=’user_nicename'”] Shortcode variations: CF7_get_current_user Current username CF7_get_current_user key=’ID’ Current user’s ID CF7_get_current_user key=’user_nicename’ Current user’s “Nice name” CF7_get_current_user key=’user_email’ Current user’s email address Referrer URL CF7_referrer Access the referrer URL, if it exists. Note […]

View

Installation 0

Install Contact Form 7 – Dynamic Text Extension just like any plugin from the repository. 1. In your admin panel, go to Plugins > Add New 2. Search for “Dynamic Text Extension” 3. Click “Install Now” on the Contact Form 7 Dynamic Text Extension 4. When the plugin finishes installing, click “Activate”

View

Quick Start 0

If you haven’t already, please install the plugin before proceeding. New features The Contact Form 7 Dynamic Text Extension adds two new “form tags” to Contact Form 7 – dynamic text and dynamic hidden. They appear beside the other tags when editing a Contact Form 7 form Adding a dynamic field Let’s walk through an example of adding a dynamic text field to a form to insert the current user’s name to demonstrate what the DTX does. 1. Set up your basic form You can create a new form or edit an existing form. In this case, we’ll create a new form as an example. Here’s the default Contact Form 7 form: We’ve added it to a page: published, that page, and can view the form on the front end 2. Find a field to replace, or add a new field In this case, let’s make the Your Email field dynamic. Now out of the box, we could set a default value for the text field and that would appear on the front end. But since not everyone is named Jim, that’s not very helpful. Instead, we want to replace this with the current user’s name. So we’ll replace the original “text” field with a “dynamictext” field instead. 3. Adding a dynamic field We’ll click the “dynamic text” button to add a new dynamic field We want it to be required, and we’ll name the field “your-name”. 4. Setting the Dynamic value This is the key to getting the current user’s name dynamically inserted into the form. In the dynamic value field, we can add any shortcode we like. In this case, we’ll use the included CF7_get_current_user shortcode. Let’s fill the field with the user’s display name, so we’ll use CF7_get_current_user key=’display_name’ Note that the square brackets [] are omitted, […]

View

Third Party Shortcodes 0

Third party shortcodes are those included in your theme or another plugin. As long as these shortcodes return a string value that can be displayed in a text input field, you can use them with the DTX.

View

Custom Shortcodes 0

If you don’t have an existing shortcode to get the value you need, you can write a custom shortcode. If you’re familiar with PHP, it’s really easy. All you do is define a shortcode tag and a function that handles processing that tag. Here’s a basic template to create a [my_special_shortcode] shortcode: Which just means that when the following shortcode is processed: [my_special_shortcode] it’ll be replaced by the text hey I’m a custom shortcode! For full details on how to write a custom shortcode, use custom attributes and content, etc, please review the following resources: Creating Custom shortcodes WordPress shortcode API

View

How do I get X value into my form? 0

You need a shortcode that returns that value. You can use one of the included shortcodes, a shortcode from a third party component like a plugin or theme, or write your own custom shortcode As long as your shortcode is written properly and it returns a string value that can be displayed in a text input, you can use it. Remember, CF7 DTX is just the conduit for putting the value returned from your shortcode into the CF7 text input.

View

Invalid Mailbox Syntax 4

If you’re using a mail tag for a dynamic text or hidden input in the To: field, you will get warnings from Contact Form 7 If you click the link provided by CF7 beside the error notice, it explains why: Invalid mailbox syntax is used In short, CF7 expects you to enter either a valid mail address, or a mail tag for an *email* form tag. The CF7 DTX form tags are not email tags. So CF7 considers these to be “invalid”. CF7 does not provide a way to register valid tags, so there is no way around the warning. But it’s just a warning, nothing is actually broken. As long as you are sure the tag you’ve created will always return a valid email address, you can safely ignore the warning.

View