Gravity Forms is perhaps the most well-known and best form builder plugin for WordPress. Not only is it easy to use and has plenty of add-ons for implementing payment or newsletter into your forms. But it’s also very developer-friendly and flexible. Personally I’ve been using and developing with Gravity Forms for at least 5 years – and I’ve never had a feature that wasn’t possible or really difficult to develop with Gravity Forms.

Keep in mind that Gravity Forms is not free. You have to purchase a license. They offer several different types with different prices in order to get and use it. Once you’ve purchased a license you also get access to a selection of their add-ons, depending on which license you purchase.

Gravity Forms for us developers

Gravity Forms offers a lot of filters and hooks, but there are no templates that you can override. The hooks offers a lot of flexibility for developers to extend and modify Gravity Forms’s behaviour. Take a look at Gravity Forms’ developers documentation pages. Most, if not all, customizations will be done onto some of Gravity Forms’ objects; either directly or through Gravity Forms API (GFAPI) class. You can also interact with Gravity Forms using REST API (which is extending WordPress’ REST API).

Example possibilities for developers are modifying field outputs, changing field’s values before they get saved as a lead, and programatically adjust email notifications. Other examples include adding custom form settings, creating new field types, and injecting custom fields or field values into existing forms.

Gravity Forms has an “Add-Ons” framework that has allowed other developers to create Add-Ons to Gravity Forms of their own. And quite a few has been made. Examples of Add-Ons are MailChimp signup, payment gateways with Stripe, Hubspot integration, Polls, Quiz, Zapier integration, and Dropbox. Which Add-Ons are available depends on your license. Take a look at an overview here.

Rendering a form

Outputting a form is done using shortcodes, e.g.:

[gravityform id="2" name="Contact us" ajax="true"]

Don’t worry, Gravity Forms will add buttons and dialogs for user-friendly embedding forms in your posts. You or the content editors won’t be required to manually type shortcodes.

But if you as a developer ever needs to embed a form programatically inside a template, all you need to know is the form ID and then simply echo the shortcode. When we output a shortcode in PHP we need to wrap it inside do_shortcode() so that WordPress converts the shortcode properly. Gravity Forms will handle adding all necessary scripts and styles for you even when you render a form with PHP.

echo do_shortcode('[gravityform id="2" name="Contact us" ajax="true"]');

Fetching form information

There are simple API functions available for fetching information, e.g. getting all forms or one specific form object. The objects are populated with all settings including all fields in the form, and you can also get all the form’s entries (form answers). Say you want to generate a <select> with all published forms offering the user to select between published forms:

// ...
$all_forms = GFAPI::get_forms();
if (!empty($all_forms)) {
	$select = '<select>';
	foreach ($all_forms as $form) {
		$select .= '<option value="' . $form['id'] . '">' . $form['title'] . '</option>';
	}
	$select .= '</select>';
}
echo $select;
// ...

To follow good coding standards, especially considering Gravity Forms is a plugin that can be deactivated or simply not exist, always check if any of Gravity Forms’ classes exist before you use them! Before the above code I would add:

if (!class_exists('GFAPI')) {
	return;
}
// Do stuff with GFAPI class

For retrieving a specific form’s object all you need is the form ID (1 in the below case):

$form = GFAPI::get_form(1);

You can even manipulate the form through the object, and then update it, for example changing the form’s title:

$form = GFAPI::get_form(1);
$form['title'] = __('New Form Title', 'txtdomain');
GFAPI::update_form($form);

Retrieving a form’s leads (answers)

Retrieving a form’s entries (form answers) for the form ID 1 is as simple as:

$entries = GFAPI::get_entries(1);

You can also retrieve a specific entry with its ID, and in the same fashion as updating the form, you can manipulate the entry object and call an update function on it to save it with your changes. The example below saves an empty string on the entry’s IP address meta, as well as the value of the field ID 2:

$entry_to_change = GFAPI::get_entry(42);
$entry_to_change['ip'] = '';
$entry_to_change['2'] = 'New value';
GFAPI::update_entry($entry_to_change);

All of the above is modifications on the very basic level. Stay tuned in the Gravity Forms category for more tutorials and more complex code functionality.