Using Gravity Forms ‘gform_preview_init’ PHP filter

The gform_preview_init action is triggered when the form preview page loads, allowing you to perform further actions.

Usage

add_action('gform_preview_init', 'my_custom_function');

Parameters

This hook has no parameters.

More information

See Gravity Forms Docs: gform_preview_init

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

Examples

Display a message on the form preview page

Add a message to the form preview page.

function display_preview_message() {
    echo "<p><strong>Note:</strong> This is a form preview.</p>";
}
add_action('gform_preview_init', 'display_preview_message');

Enqueue a custom script for the form preview page

Enqueue a custom JavaScript file only on the form preview page.

function enqueue_preview_script() {
    wp_enqueue_script('my_custom_script', 'path/to/your/script.js', array('jquery'), '1.0.0', true);
}
add_action('gform_preview_init', 'enqueue_preview_script');

Modify the preview page title

Change the title of the form preview page.

function modify_preview_title() {
    add_filter('wp_title', function ($title) {
        return "Form Preview - " . $title;
    });
}
add_action('gform_preview_init', 'modify_preview_title');

Redirect users who are not admins

Redirect users who are not admins away from the form preview page.

function restrict_preview_access() {
    if (!current_user_can('manage_options')) {
        wp_redirect(home_url());
        exit;
    }
}
add_action('gform_preview_init', 'restrict_preview_access');

Add custom CSS for the form preview page

Enqueue a custom CSS file only on the form preview page.

function enqueue_preview_styles() {
    wp_enqueue_style('my_custom_styles', 'path/to/your/styles.css', array(), '1.0.0');
}
add_action('gform_preview_init', 'enqueue_preview_styles');