The gform_get_form_filter Gravity Forms PHP filter allows you to modify the form’s HTML after it has been generated but before it is displayed.
Usage
Applies to all forms:
add_filter('gform_get_form_filter', 'your_function_name', 10, 2);
To target a specific form, append the form ID to the hook name (format: gform_get_form_filter_FORMID):
add_filter('gform_get_form_filter_10', 'your_function_name', 10, 2);
Parameters
- $form_string (string): The form markup, including the init scripts.
- $form (Form Object): The form currently being processed.
More information
See Gravity Forms Docs: gform_get_form_filter
Examples
Hide form for logged-in users
Hide the form if the user is already logged in:
add_filter('gform_get_form_filter_1', function($form_string, $form) { if (is_user_logged_in()) { $form_string = "<p>This form is for new users, as you are already registered you don't need to use it again.</p>"; } return $form_string; }, 10, 2);
Hide form on a specific day of the week
Hide the form on Mondays:
add_filter('gform_get_form_filter_1', 'custom_schedule', 10, 2); function custom_schedule($form_string, $form) { $day = date('l'); if ($day == 'Monday') { $form_string = '<p>We are closed today, please return tomorrow to make your booking.</p>'; } return $form_string; }
Remove a form class conditionally
Remove the gf_simple_horizontal class for a form with ID 63 if the page ID is not 9098:
add_filter('gform_get_form_filter_63', function($form_string, $form) { if (!is_page(9098)) { $form_string = str_replace('gf_simple_horizontal', '', $form_string); } return $form_string; }, 10, 2);
Add custom content to form
Add custom content after the form:
add_filter('gform_get_form_filter_1', function($form_string, $form) { $custom_content = '<p>Custom content goes here.</p>'; $form_string .= $custom_content; return $form_string; }, 10, 2);
Wrap form in custom container
Wrap the form in a custom <div>
container:
add_filter('gform_get_form_filter_1', function($form_string, $form) { $form_string = '<div class="custom-container">' . $form_string . '</div>'; return $form_string; }, 10, 2);
This code should be placed in the functions.php file of your active theme.