Advertisement
  1. Code
  2. Plugins
Code

Robust Forms With Gravity Forms

by
Difficulty:IntermediateLanguages:

Gravity Forms, a rocketgenius product, is a comprehensive form solution for WordPress. Any developer will tell you that building a form can sometimes be very time intensive, especially if it needs to do more than handle a message.

What if you need a form that registers users to your website based on certain criteria? Or what if you need a form that allows a user to post content to your website, from a front-end perspective? Gravity Forms can do all of this, and what it can't do out-of-the-box you can build yourself, utilizing its built in hooks and filters.

Before I discuss how Gravity Forms can extend your WordPress installation, it's important to note that Gravity Forms is a premium plugin. To get some of the goodies discussed in this post you'll need at least a Business License for Gravity Forms, however I strongly recommend the Developer License as it provides the most bang for your buck. Now that we've settled that, let's take a look at a few ways Gravity Forms can extend a WordPress site.


Extending User Registration Forms

It's possible to add extra fields to your registration form by adding code to your functions.php file, but you can avoid spending too much time under the hood by letting Gravity Forms do the heavy lifting. Creating users and assigning user roles with Gravity Forms is easy, you simply create a form, match up your fields with the WordPress default fields and pick the user role to be assigned upon submission. You can also have multiple "registrations" for the same form.

One example of how Gravity Forms can extend the user registration process is with conditional registration. For example, if you have a membership site that only allows people with '@xyz.com' and '@abc.com' email addresses to register, you could add registration feeds that only create and assign users to roles based on whether they meet that criteria, while not allowing others to register.

This conditional logic goes well beyond user registration, you can create conditional fields within any type of form. For example, if you wanted to show additional fields to a user based on their age group, you could customize the field by selecting to show (or optionally to hide) other fields based on their response.

conditional-screenshot

Using Gravity Forms for Front-End Content Submission

If you want a more robust front-end for your users, Gravity Forms can be a great solution by allowing users to submit content to posts directly. All you need to do is create a form that uses the built in post fields feature. These post fields map to the default post title, body content, excerpt, feature image, category and tag. Gravity Forms also maps form data into custom fields. To map your content to a custom field:

custom-field
Don't see your custom field listed in the drop down? Go back to your posts and create a "draft" that includes content in the custom field and save it. Then go back to your Gravity Form and check again.

When Gravity Forms is used in conjunction with the plugin, Gravity Forms + Custom Post Types, the combination offers even more functionality by allowing form data to be mapped to custom post types, hence the name of the plugin. If you're worried about installing too many plugins - and only need support for custom taxonomies - you can always add support manually by hooking into Gravity Forms via your functions.php file.


Speaking of Hooks and Filters

Gravity Forms comes with extensive documentation. If you can dream it, you can probably accomplish it. There are several hooks and filters you can take advantage of, and the best part is you can assign them to individual forms. Suppose you wanted to allow users to submit posts to your site, but you wanted to control their title length, you could create custom validation logic to limit the length of the input. See example.

The first part of code uses the filter for Gravity Forms validation and creates a function that tests the length of the field in question. Pay attention to the numbers 7 and 3 within this snippet. 7 denotes the form ID and 3 denotes the actual input ID being tested.

The Gravity Forms documentation includes a list of available hooks and filters, as well as detailed examples for each - occasionally you can even see when / why you might need them.


Conclusion

If you're looking for an out-of-the-box form solution that does more than send an email, Gravity Forms is a great option - and time saver. The possibilities are limitless. For those debating or on the fence about using Gravity Forms due to purchase price, it may seem high, but the benefits and development timed saved by using Gravity Forms pay for itself.

Advertisement
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.