Contact form 7 Developer

Form Variables

$_GET: {{ _GET }}

$_POST: {{ _POST }}

$_SERVER: {{ _SERVER }}

$_REQUEST: {{ _REQUEST }}

User: {{ user }}

Example:

{{ user.id }} {{ _GET.utm_source }}

Validation Variables

$validate: {{ validate }}

$form: {{ form }}

Example:

{% if form['contact-name'] is empty %}
{% set validate = validate|merge({'contact-name': 'Your name is invalid'}) %}
{% set validate = validate|merge({'form': 'This form is invalid'}) %}
{% endif %}

Email Variables

$submission: {{ submission }}

Example:

{{ submission['contact-name'] }}

To use dump() in twig, you will need to enable WP_DEBUG, or else dump will not work.

Sample 1 (validate contact-name and contact-email)

{% if form['contact-name']|trim == '' %}
    {% set validate = validate|merge({'contact-name': 'Please enter a valid name'}) %}
{% endif %}
{% if '@' not in form['contact-email'] %}
    {% set validate = validate|merge({'contact-email': 'Please enter a valid email'}) %}
{% endif %}
{% if validate is empty %}
{% set form = form|merge({'contact-name': '', 'contact-email': ''}) %}
{%  endif %}

Sample 2 (validate message length)

You still have {{100 - form['your-message']|length}} characters

Sample 3 (show city based on country select)

<label>City</label>
{% if form['country'] == 'USA' %}<select>
<option value="USA1">USA city 1</option>
<option value="USA1">USA city 2</option>
</select>
{% else %}<select>
<option value="USA1">Canada city 1</option>
<option value="USA1">Canada city 2</option>
</select>
{% endif %}