Gravity Forms is a great way to build all sorts of forms in WordPress. It can be so simple that non-programmers can easily build their own basic forms. It’s also incredibly flexible so programmers can extend it in myriad ways, even by adding some complex custom fields of their own. But as usual, there’s a trade-off — ease of use for non-programmers means lots of hoops to jump for programmers. Here’s an overview of how I built some fairly complex compound fields for a custom application using Gravity Forms.
Update: this post was written before the Gravity Forms add-on framework was a thing. Everything is much simpler now using that framework, and its field framework, so please refer to that now. This post is preserved for historical amusement only!
First off, I have to give props to Travis Smith for his tutorial for building custom fields in Gravity Forms. This tutorial gave me an incredible kick-start on the path to building custom fields. If you want to know how to build custom fields for Gravity Forms, go read Travis’ article now! Go on, I’ll wait.
You’re back? Good. Now, I’m not going to get into details, since you’ve already Travis’ article and know the basics. What I’m going to do here is list the hooks I used, and why. What I built was a couple of pretty complex fields that needed to support multiple front-end form inputs per field, nicely formatted listings and detail views at the back end, CSV exports, and calculations. If I explained it all in detail, it’d take me all night. Each hook is linked to the Gravity Forms documentation, so you can get more detail there.
So, to the hooks.
- gform_add_field_buttons — add field buttons on the forms editor
- gform_field_css_class — add a CSS class to the LI wrapping the field (which can help with styling)
- gform_enqueue_scripts — enqueue additional scripts and stylesheets required by the field
- gform_editor_js_set_default_values — set default field values
- gform_field_standard_settings — add custom inputs to the field in the form editor, e.g. custom labels, field options
- gform_field_type_title — modify the field’s title
- gform_field_input — modify the field’s input tag (this is where you create all the inputs, selects, textareas, etc. for your custom field)
- gform_tooltips — add custom tooltips for the field on the form editor
- gform_field_validation — validate inputs for the field
- gform_save_field_value — encode the field’s values ready for saving to the database
- gform_get_input_value — get field value from long details table, if set (because by default, Gravity Forms only gets up to 200 characters when listing / viewing entries, and compound fields can easily exceed that)
- gform_entry_field_value — format the field’s values for viewing an entry in detail
- gform_entries_field_value — format the field’s values for a list of entries
- gform_merge_tag_filter — format the field’s values for emails (yes, really, this hook!)
- gform_export_field_value — format the field’s values for CSV export
- gform_pre_render — perform calculations on values buried inside compound fields, and set results in fields with our dynamic population parameter names