gform_get_form_filter

Description

This filter allows you to override 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

Examples

1. Hide form for logged in users

This example demonstrates how to hide the form if the user is 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 );

Anonymous functions (closures) as shown in the example above are only compatible with PHP 5.3 and newer. If you are using an older version of PHP use a function like in the example below.

2. Hide form on a specific day of the week

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;
}

3. Remove a form class conditionally

This example demonstrates how to remove the gf_simple_horizontal class for a form with ID 63 if the ID for the page where the form is being displayed is not 9098. You need to edit these values to make it work for your form/page.

add_filter( 'gform_get_form_filter_63', function ( $form_string, $form ) {
	// Remove gf_simple_horizontal if page ID is not 9098.
    if ( ! is_page( 9098 ) ) {
        $form_string = str_replace( 'gf_simple_horizontal', '', $form_string );
    }
 
    return $form_string;
}, 10, 2 );

Placement

This code should be placed in the functions.php file of your active theme.

Source Code

This filter is located in the GFFormDisplay::get_form() in form_display.php