Form action adds form processing to the enclosed HTML form.
Imagine we have a nice HTML form on our contact page and we want to store and email form submissions through WordPress.
Normally, that would mean going through a bunch of WordPress form plugins and struggling with recreating our form in their form builders. At the end, the result will never match our source HTML form because all these plugins enforce their own styling and HTML structure.
Wouldn’t it be nice to be able to keep our HTML form as it is and just add submission processing?
That’s exactly what the Form smart action does.
It lets us send submissions with email and also store them as private custom post type posts viewable through the WordPress admin panel.
We can tell it which element to show when the form is successfully submitted and which element to show when it fails.
It does all of this without using any WordPress plugins.
Here’s how it works:
Add the Form action on the element that contains the <form> and any supporting elements, like elements that are shown in case of successful submission or failure.
Of course, we can also just add it to the <form> element directly, if we don’t have any form-related elements outside of the <form> element.
Next, we have to tell the Form action what to do with our form:
First, we need to give the form a unique id that will be used to process the form under the hood. “Contact_form” is used by default. No need to set a different id if this is the only form on the page.
Send submissions to email
Check “Send to email” to have submitted form information sent by email.
In Email options, enter the email address if it is different from the site admin email address.
Built-in WordPress function wp_mail will be used to email the form information. That might not work, depending on the server configuration – and especially on local WordPress installs. In such cases we can add “WP Mail SMTP” WordPress plugin that lets us configure a mail account for sending email.
Note, no email is sent to the user who is submitting the form.
Save submission as post
Submitted information can also be stored directly to WordPress database, either as a backup in case of email problems or as the only method of storing the information.
Selecting this option will save each submission as a private custom type post that can be viewed through WordPress admin panel.
Post Type options let us select a custom post type for submissions and optionally register a new post type.
Such posts are not publicly accessible – they are saved as private.
Texts & Messages
Next, we get the opportunity to customize texts used in emails and saved posts as well as texts or elements that are shown after the form has been processed.
Email / post title is used for email subject and saved post titles.
Message intro text is displayed at the beginning of the email or saved post.
Success element selector selects the element that is used when the form is submitted successfully. Note that we are responsible for styling this element.
If that is not set, the text in Success message is displayed.
Error message selector points to the element that is shown in case of failures. Note that we are responsible for styling this element.
Error message text is used if error element is not set.
Checking “Show submitted data to the user” will show the submitted information after the form is successfully processed.
Check “Use ReCaptcha v2” to add the reCaptcha check to the form. The control is shown in an element with the class “g-recaptcha”. If that element doesn’t exist in the form it will be inserted right before the submit button.
Click on the “Get the API key & secret” to go to the Google page for registering a reCaptcha form. Make sure to select the version 2.
We have two ways of storing the reCaptcha key and secret:
Storing reCaptcha API key and secret
They can be hard-coded in the theme by entering into them into Key and Secret fields. That’s not the recommended approach.
Alternatively, check the “Use Customizer to store API key and secret” in fields in Captcha settings Customizer section.
That means that every user of the theme can enter their own key and secret.
Note, it would be better to store API key and secret in WordPress settings. We might do that in the future, for now the recommended approach is to use the customizer.
Note, some input field names are reserved by WordPress. Such fields will be renamed by appending the “_field” suffix to their names.