Using Gravity Forms ‘gform_preview_body_open’ PHP action

The gform_preview_body_open action is triggered when the body of the form preview page loads, allowing further actions to be done.

Usage

add_action('gform_preview_body_open', 'my_function', 10, 1);

Parameters

  • $form_id (int) – The ID of the current form being previewed.

More information

See Gravity Forms Docs: gform_preview_body_open

This action hook was added in Gravity Forms 2.4.19 and is located in preview.php.

Examples

Add a custom message to the form preview page

Add a custom message at the beginning of the form preview page.

function add_custom_message() {
    echo '<p><strong>Welcome to the form preview!</strong></p>';
}
add_action('gform_preview_body_open', 'add_custom_message', 10, 1);

Display form ID on the preview page

Display the form ID at the beginning of the form preview page.

function show_form_id($form_id) {
    echo '<p>Form ID: ' . $form_id . '</p>';
}
add_action('gform_preview_body_open', 'show_form_id', 10, 1);

Add custom CSS class to the body tag

Add a custom CSS class to the body tag on the form preview page.

jQuery(document).on('gform_preview_body_open', function(event, form_id) {
    jQuery('body').addClass('my-custom-class');
});

Add a logo to the form preview page

Insert a logo at the beginning of the form preview page.

function add_logo() {
    echo '<img src="https://example.com/logo.png" alt="Logo" />';
}
add_action('gform_preview_body_open', 'add_logo', 10, 1);

Hide specific fields on the form preview page

Hide specific form fields on the form preview page based on the form ID.

function hide_specific_fields($form_id) {
    if ($form_id == 3) {
        echo '<style>.gfield_id_2, .gfield_id_4 { display: none; }</style>';
    }
}
add_action('gform_preview_body_open', 'hide_specific_fields', 10, 1);