Using Gravity Forms ‘gform_shortcode_preview_disabled’ PHP filter

The gform_shortcode_preview_disabled Gravity Forms filter is used to enable or disable the shortcode preview within the Add Form button.

Usage

To use this filter, add the following code to your theme’s functions.php file:

add_filter('gform_shortcode_preview_disabled', 'your_function_name');

Parameters

  • $preview_disabled (bool): Determines if the shortcode preview should be disabled. Defaults to true (disabled).

More information

See Gravity Forms Docs: gform_shortcode_preview_disabled

Examples

Enable the shortcode preview

To enable the shortcode preview, return false in your custom function:

add_filter('gform_shortcode_preview_disabled', 'enable_shortcode_preview');

function enable_shortcode_preview() {
    // Enables the shortcode preview
    return false;
}

Disable the shortcode preview

To disable the shortcode preview, return true in your custom function:

add_filter('gform_shortcode_preview_disabled', 'disable_shortcode_preview');

function disable_shortcode_preview() {
    // Disables the shortcode preview
    return true;
}

Enable shortcode preview for admin users only

Enable the shortcode preview only for users with the administrator role:

add_filter('gform_shortcode_preview_disabled', 'enable_shortcode_preview_for_admin');

function enable_shortcode_preview_for_admin() {
    // Check if the user is an administrator
    if (current_user_can('administrator')) {
        return false;
    }

    return true;
}

Enable shortcode preview based on user capability

Enable the shortcode preview for users who can edit_posts:

add_filter('gform_shortcode_preview_disabled', 'enable_shortcode_preview_for_editors');

function enable_shortcode_preview_for_editors() {
    // Check if the user can edit posts
    if (current_user_can('edit_posts')) {
        return false;
    }

    return true;
}

Enable shortcode preview based on a custom condition

Enable the shortcode preview based on a custom condition, such as the user’s email domain:

add_filter('gform_shortcode_preview_disabled', 'enable_shortcode_preview_based_on_email');

function enable_shortcode_preview_based_on_email() {
    $current_user = wp_get_current_user();
    $email_domain = substr(strrchr($current_user->user_email, "@"), 1);

    // Enable the shortcode preview for users with a specific email domain
    if ($email_domain == 'example.com') {
        return false;
    }

    return true;
}