Skip to content
pinegrow.com
Docs menuWordPress

Form

Form action adds form processing to the enclosed HTML form.

On this page

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.

Element tree for a contact form with div.contact-form-wrap, the form element, its labels and inputs, and the status message blocks.

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.

Form settings

Next, we have to tell the Form action what to do with our form:

Form Smart panel with Form id set to the default contact_form and Send to email and Save as post type options.

Form id

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.

Form Smart panel with Send to email checked and the email address info@pineadventures.com entered.

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.

Form Smart panel with Save as post type checked, Post type set to contact and Register custom post type checked, named Contacts.

Selecting this option will save each submission as a private custom type post that can be viewed through WordPress admin panel.

WordPress Edit Post screen under Contacts, with the title Someone is in touch and the saved submission from Mr. Adventure Bear.

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.

Form Texts and Messages settings with Success and Error element selectors pointing to the status message blocks in the element tree.

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.

Contact form page showing the green message Thank you! Your submission has been received! under the Want to get in touch heading.

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.

Contact form with a reCAPTCHA checkbox and a red message reading Oops! Something went wrong while submitting the form.

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.

Contact page after submission with the green thank you message and the submitted name, email and message shown below it.

Using Captcha

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.

Form CAPTCHA settings with Use ReCaptcha v2 checked and empty Key and Secret fields flagged as required, with a Get the API key and secret button.

Alternatively, check the “Use Customizer to store API key and secret” in fields in Captcha settings Customizer section.

Form CAPTCHA settings with Use ReCaptcha v2 and Use Customizer to store API key and secret both checked.

That means that every user of the theme can enter their own key and secret.

WordPress Customizer Captcha settings showing the ReCaptcha API Key and Secret fields, beside a contact form with the reCAPTCHA checkbox.

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.

Advanced

Clear JS submit handlers

Some websites come with custom Javascript code that handles the form submissions. That prevents the form to be processed by WordPress. Checking this option will try to remove such handlers from the form.

Form Advanced section with the Clear JS submit handlers option set to Yes.

Call function

In Pinegrow PRO you can add the name of a function to call when the form is submitted successfully. Data is passed as the first argument, stored in the array. Form options array is passed in the second argument. The functions should return true on success and false on failure.

For example, set the Call function to process_my_form if the function is defines as:

function process_my_form($data, $options) {
    //do some cool things with the submitted data
    return true; //all good
}

Note, the Call function can not be used to redirect the user to a different page because at the time the function is called, HTTP headers have already been sent.

Customizing the email message

Use the WordPress filter pg_form_email to customize the email message and title. Inspect the inc/wp_simple_form_mailer.php to see how it works and what kind of data it gets.

A note about input field names