Using Gravity Forms ‘gform_field_container’ PHP filter

The gform_field_container filter allows you to modify the markup (the li element) used for the field container in Gravity Forms.

Usage

A generic example for using the filter:

add_filter('gform_field_container', 'your_function_name', 10, 6);

Parameters

  • $field_container (string) – The field container markup. The placeholder {FIELD_CONTENT} indicates where the markup for the field content should be located.
  • $field (Field Object) – The field currently being processed.
  • $form (Form Object) – The form currently being processed.
  • $css_class (string) – The CSS classes to be assigned to the li element.
  • $style (string) – An empty string as of 1.9.4.4. Was previously used to hold the conditional logic display style.
  • $field_content (string) – The markup for the field content (label, description, and inputs, etc.) which will replace the {FIELD_CONTENT} placeholder.

More information

See Gravity Forms Docs: gform_field_container

Important: Do not return li tags when legacy markup is disabled. Removing attributes from the container tag can break features of the form editor, such as ajax saving.

Examples

Add a custom class to all field containers

This example adds a custom CSS class to all field containers in all forms.

add_filter('gform_field_container', 'add_custom_class_to_field_container', 10, 6);
function add_custom_class_to_field_container($field_container, $field, $form, $css_class, $style, $field_content) {
    return str_replace(' class="', ' class="some-custom-class ', $field_container);
}

Add a custom class to field containers in a specific form

This example adds a custom CSS class to all field containers in a specific form (form ID 10).

add_filter('gform_field_container_10', 'add_custom_class_to_field_container_form_10', 10, 6);
function add_custom_class_to_field_container_form_10($field_container, $field, $form, $css_class, $style, $field_content) {
    return str_replace(' class="', ' class="some-custom-class ', $field_container);
}

Add a custom class to a specific field container in a specific form

This example adds a custom CSS class to a specific field container (field ID 3) in a specific form (form ID 10).

add_filter('gform_field_container_10_3', 'add_custom_class_to_field_container_form_10_field_3', 10, 6);
function add_custom_class_to_field_container_form_10_field_3($field_container, $field, $form, $css_class, $style, $field_content) {
    return str_replace(' class="', ' class="some-custom-class ', $field_container);
}

Modify the markup of all field containers

This example replaces the li element with a div element for all field containers in all forms.

add_filter('gform_field_container', 'modify_field_container_markup', 10, 6);
function modify_field_container_markup($field_container, $field, $form, $css_class, $style, $field_content) {
    return str_replace(array('<li', '</li>'), array('<div', '</div>'), $field_container);
}