How to set up contact form 7 in WordPress. All fields (tags) of the form. Spam protection with Really Simple CAPTCHA

Contact Form 7
  • All form fields (tags)
  • How to make one of the fields mandatory: mail or phone
  • Conditional Fields for Contact Form 7 addon
  • Contact form 7 Custom validation addon

In the previous article, we got acquainted with the Contact Form 7 plugin and its settings. And here, all supported field types (form template tags) and the parameters of each of them, as well as styles (CSS classes) of form elements will be discussed in detail. This is unofficial documentation for all Contact Form 7 form template tags.

The field type determines the type of HTML form element: text, “radio”, “select”. The field type is indicated in the name of the form template tag.

An example of a text field tag and the HTML code it will output:

Also, each tag (field) has its own options (tag attributes). Tag options are specified after the title and field name. For example, let's add a class attribute for the previous text field:

Form template tag (field) syntax:

Tag (field) attributes

You can use the same attributes (options) in different form tags. Therefore, in order not to repeat the description of each attribute for each form field, we will place their description in a separate section.

Attributes that can only be applied to a specific form tag will be listed separately for each tag.

* The * immediately after the field type makes the field mandatory.
Example: id (line) Identifier name. css attribute id, used to design the field.
Example: id:foo class (line) Class name. css class attribute, used to design the field.
Example: class:bar
You can specify several classes: minlength and maxlength (number)

These are HTML5 attributes that limit the input of characters into a field, both by a lower threshold (minimum) and an upper threshold (maximum). The user will not be able to enter fewer characters (minlength) or more characters (maxlength) into the field than specified by these values.

For minlength , if the entered text is less than this value, the user will receive a "Field is too short" warning. And vice versa for maxlength .

Examples:
minlength:10 -
maxlength:90 -
both at once -

As a result, we get a text field that will issue a warning and the letter will not be sent if less than 10 or more than 140 characters are entered into it.

These parameters support the following field types: text, textarea, email, url, tel, quiz.

There is an old entry for these parameters:

The maxlength parameter explicitly takes precedence, so if you add maxlength:150 , then 140 will be rewritten to 150.

Size (number) The width of the text field, which is determined by the number of characters in a monospace font. Can be overridden in CSS styles using the width attribute.
Example: size:50 -
Default: 40 default (string/number)

Default field value.

Example: default:value.

If the user filling out the form is authorized, then by default you can substitute the value taken from his profile data. For example, if you specify the attribute default:user_email - , then the field will automatically be filled with the value - “email of the current user” who is viewing the form...

Other special fields of the default attribute:

    default:post_meta- will get the value of the metafield whose key will be equal to the field name. For example: will display a form field whose value will be set to the value of the post metafield mymeta_key

    default:get- will set the value of the GET parameter with the key equal to the field name: - http://example.com/?mykey=value

    default:post- will set the value of the POST parameter with the key equal to the field name: - in POST we pass the data mykey=value.

    default:shortcode_attr- will set the value of the shortcode attribute with the key equal to the field name. For example, the form template tag is: , then in the shortcode of the form itself, when called in the post content, we specify the mykey attribute: . Read more below.

    Also, you need to add the wordpress shortcode attribute name to the whitelist (because wordpress strips out all unknown shortcode attributes...). This is done through a filter:

    Add_filter("shortcode_atts_wpcf7", "my_shortcode_atts_wpcf7", 10, 3); function my_shortcode_atts_wpcf7($out, $pairs, $atts)( if(isset($atts["mykey"])) $out["mykey"] = $atts["mykey"]; return $out; )

For checkbox, radio and select:

  • default:number - default:2- selects the second element of the list...
  • default:number_number - default:2_3- selects the second and third element of the list (for multi-selects and checkboxes)...

Read more about default and its options, read at the end of the article.

Placeholder or watermark (line)

The text that will be shown in the placeholder attribute.

This option must be used at the end of all template tag options: and not

Can be used watermark instead of placeholder, this is a synonym (alias) of the attribute.

Akismet (line)

Spam protection. Requires the Akismet plugin installed and activated.

  • akismet:author -
  • akismet:author_email -
  • akismet:author_url -

How it works? When sending data, the values ​​of the specified fields will be processed on the akismet server and if they do not pass the check, the letter will not be sent, and the user will see the error:

The message will disappear when you focus on any of the fields.

Form template tags (fields)

Below are all the plugin tags that you can use in your form template. These tags will become HTML input, textarea, and select elements.

Text fields include the following types: text, email, url, tel, textarea, and the count tag is closely related to text fields.

There are two types of numeric fields: number and range. These are HTML5 fields, so older browsers do not support them, displaying a text field instead.

text

One line text field.

Supported options (attributes):

  • class
  • minlength And maxlength
  • size
  • placeholder
  • default- you can use: user_login, user_first_name, user_last_name, user_nickname, user_display_name
  • akismet:author

email

The field is intended for entering email.

Supported attributes:

  • class
  • minlength And maxlength
  • size
  • placeholder
  • akismet:author_email
  • default:user_email or default: [email protected]

url

Field for entering the Internet address (URL).

Supported attributes:

  • class
  • minlength And maxlength
  • size
  • placeholder
  • akismet:author_url
  • default:user_url or default:value

tel

Field for entering a phone number.

Supported attributes:

  • class
  • minlength And maxlength
  • size
  • placeholder

textarea

Field for entering multi-line text.

Supported attributes:

  • class
  • minlength And maxlength
  • placeholder
  • (cols)x(rows)- where cols is the width of the field in characters, and rows is the height of the field in lines of text. Has 3 formats: (cols)x(rows) , (cols)x and x(rows) .

For textarea, the field value can be specified in two ways:

  1. Default text...

If the textarea contains html tags, then after submitting the form you may receive a 500 server error!

count (character counter)

The count tag allows you to set a counter when you want to show the user how many characters have been entered or how many characters are left until the maximum value (if the parameter is maxlength).

To activate the counter, you need to insert a counter tag into the form template indicating the name of the text field for which it will count characters.

The counter tag can be used anywhere in the form template.

For example, we have a text field for a message:

Then we do this:

The field is limited to a minimum of 10 and a maximum of 140 characters. In this case, the counter will show how many characters have been entered.

Sometimes it is more convenient to show how many characters are left to enter, because the user does not know about the maximum limit. To do this, you need to specify the down - option in the tag.

number

Field for entering a number. When the field has focus, values ​​can be changed by rotating the mouse wheel.

Supports attributes:

  • class
  • placeholder
  • min
  • max

range

Slider to select numbers in the specified range.

Supports attributes:

  • class
  • placeholder- are displayed only in browsers that do not support this type of field and display a text field instead.
  • min- Sets the lower value for entering a number: min:20
  • max- Sets the upper value for entering a number: min:100
  • step- Sets the number change step for sliders and number input fields: min:5

checkbox and radio

  • Radio buttons- These are switches that allow you to select only one option from several.
  • Checkboxes- These are checkboxes that allow you to select several options at once.

Supported attributes:

  • default- The default value of the field, for example default:2, means that when the field is displayed, the second answer option will already be selected. For checkboxes you can specify several values ​​like this default:1_2_3

    first_as_label- By default, the checkbox (radio button) comes first, and then its (her) inscription. By passing this parameter, you will swap them.

    use_label_element- Wraps a checkbox or radio button in a label, which allows you to select an element by clicking on its name.

    exclusive- By specifying this parameter, only 1 element can be selected from the checkbox. This functionality is based on JavaScript. Radio buttons do not support this option due to their specific nature.

    free_text- Allows you to add a text field to the last element. It becomes available for completion when the last element is selected and the user can supplement his answer.

  • data- Gets a value from a sheet with countries of the world, for example data:countries .

China India San Marino

select

Allows you to select answer options from a drop-down list. Can be used in two ways:

Attributes:

  • class
  • default- default:2 or default:2_3_4
  • multiple- Allows you to select multiple answer options
  • include_blank- Add the line "- - -" as the first answer option
  • first_as_label
  • data
  • items- answer options

date

Field for selecting a calendar date. This is an HTML5 type and is not supported by older browsers.

Attributes:

  • class
  • placeholder
  • min- Lower value of a possible date, for example min:2016-01-01 . Relative date format is supported, for example today+10days , today-2weeks and so on.
  • max- The upper value of a possible date, for example min:2017-01-01 . Relative date format is supported, just like min .
  • step- Date change step in days, for example step:3 . If the lower value is 2016-01-01 and step 3, then the next step will display the date 2016-01-04 and so on.

When you insert a tag into a letter template, you will receive a date in the mail in the format YYYY-MM-DD (2016-12-25) - this is not clear. Therefore, you can specify a more visual template; to do this, use the construction [_format_(field name) "(date format)"]

[_format_your-date "D, d M y"]

reCAPTCHA (spam protection)

Contact Form 7, starting from version 4.3, in conjunction with the reCAPTCHA service, allows you to fight spam. To use reCAPTCHA you need to have a Google account, where you can get keys to use the service API.

To get these keys you need:

  1. Go to reCAPTCHA admin panel.
  2. Register a site.
  3. Get a site key and a secret key (these are two different keys: there can be one site key, but there are many secret keys).

Attributes:

  • class
  • theme- Widget color scheme. Can be dark or light (default).
  • size- Widget size. Can be compact (compact) or normal (default).

Now in the form template use the tag.

Quiz (question-answer)

In this field, the user is asked a question and must answer it, otherwise the letter will not be sent. The field appeared when spam was fought using letter captchas (there was no reCAPTCHA yet). The use of letter captchas imposed restrictions both on the server (graphic libraries were required) and on the person (visual perception errors). This method solved these shortcomings.

Attributes:

  • class
  • size- Field size and length. One of the values ​​may be omitted.

The example shows that the question and answer are separated by the sign | . The question and answer can be written in absolutely any way, for example mathematical:

You can specify several question-answer pairs; the question will be selected randomly each time the form is displayed:

For this field, you can specify the minlength and maxlength parameters:

When creating a quiz tag with multiple questions using a generator, write each question-answer pair on a new line.

hidden

Hidden field - not visible to the user. Can store valuable information. Information can be added using Javascript or created using the default option.

Attributes:

  • class
  • default- You can pass a parameter here that will be converted according to a specific rule. Read more in the attribute description (options at the beginning of the article).

submit

Button for sending form data (submit). When you click this button, all form data is sent to the server.

Attributes:

  • class

You can specify the button text:

An example of using all supported parameters:

ajax-loader

Please note that the tag in which the download icon is loaded if you click on submit the form, it is added along with the submit button, or rather after it.

So, for example, if you replace the tag with a ready-made HTML button. This is sometimes needed when the button layout is not suitable. For example, if you want the submit button to look like this: