Adding a Form Using a Theme File or Hooks

Summary

In this article, we show how to use the Gravity Forms function call embedding method that would allow you to add a form to a page or post using a theme file or third-party hooks.

Function Call

If you would like to call a form from within a WordPress theme file, you may do so using a function call. The function and its available parameters are outlined below.

gravity_form( $id_or_title, $display_title = true, $display_description = true, $display_inactive = false, $field_values = null, $ajax = false, $tabindex, $echo = true, $form_theme = null, $style_settings = null );
  • $id_or_title
    (mixed) (required) The id or title of the form to be embedded.
  • $display_title
    (boolean) (optional) Whether or not to display the form title.
    Defaults to true.
  • $display_description
    (boolean) (optional) Whether or not to display the form description.
    Defaults to true.
  • $display_inactive
    (boolean) (optional) Whether or not to display the form even if it is inactive.
    Defaults to false.
  • $field_values
    (array) (optional) Pass an array of dynamic population parameter keys with their corresponding values to be populated.
    Example: “array(‘parameter_name’ => ‘custom_value’)”
    Defaults to false.
  • $ajax
    (boolean) (optional) Whether or not to use AJAX for form submission.
    Defaults to false.
  • $tabindex
    (integer) (optional) Specify the starting tab index for the fields of this form.
  • $echo
    (boolean) (optional) Whether to echo the form code or return it.
    Defaults to true.
  • $form_theme
    (string) (optional) Specify the form theme to be used.
    Defaults to null inheriting from Default Form Theme.
  • $style_settings
    (JSON string) (optional) JSON-encoded style settings. Passing false will bypass the gform_default_styles filter.
    Defaults to null.

Usage Examples

Basic Function Call

gravity_form( 1, false, false, false, '', false );

This snippet will display the form with an id of ‘1’; the title and description will not be displayed, the form itself will not display if it is inactive, and it will not use AJAX for form submission.

With Ajax & Tabindex

gravity_form( 1, false, false, false, '', true, 12 ); 

This snippet will display the exact same form as above, except it will use AJAX for form submission and the starting tabindex will be 12.

Using the Form Title instead of ID

gravity_form( 'Contact Us', false, false, false, '', false );

This snippet will display the form titled ‘Contact Us’; the title and description will not be displayed, the form itself will not display if it is inactive, and it will not use AJAX for form submission.

If you use the form title or id for a form that does not exist, the following message will be displayed:

Oops! We could not locate your form.

Pass a variable for Dynamic Population

gravity_form( 1, false, false, false, array('some_field' => $my_variable ), false);

This snippet will do the same shown above for Basic Function Call adding the array to pass a variable named $my_variable that will populate a field that has the parameter name some_field

Enqueue Scripts and Styles

When embedding a form via a function call, you must manually include the necessary Gravity Forms-related Javascript and CSS via the built-in WordPress enqueue capabilities. Gravity Forms does not include these by default when calling a form via a function call, and they are necessary for forms that contain conditional logic or the date picker field.

We strongly recommend you enqueue the scripts rather than including them as hardcoded calls in your theme. Implementing it this way will ensure that Gravity Forms does not include them on the page if they are already present. It is also a good practice only to load these scripts on the front end.

Gravity Forms 1.5 introduced the gravity_form_enqueue_scripts function, which allows you to easily enqueue the necessary Gravity Forms’ scripts and styles when manually embedding a form. This is also useful if you are using a GF widget and do not wish for the styles and scripts to be loaded inline.