The gform_force_hooks_js_output filter in Gravity Forms PHP can be used to prevent the Gravity Forms scripts from being added to all pages.
On this pageJump to a section
Usage
To use this filter, simply add the following line to your code:
add_filter('gform_force_hooks_js_output', 'your_function_name');
Parameters
- Defaults to
false. Set totrueusing the WordPress__return_truefunction or a custom function that returnstrue.
More information
See Gravity Forms Docs: gform_force_hooks_js_output
This filter was added in Gravity Forms version 2.5.3.1.
Examples
Preventing Gravity Forms scripts from being added to all pages
// Prevent Gravity Forms scripts from being added to all pages
add_filter('gform_force_hooks_js_output', '__return_true');
Allow Gravity Forms scripts only on specific pages
// Function to check if the current page is a specific page
function allow_gform_scripts_on_page() {
// Replace PAGE_ID with the actual page ID where you want to allow Gravity Forms scripts
return (is_page('PAGE_ID')) ? true : false;
}
// Prevent Gravity Forms scripts from being added to all pages except the specified page
add_filter('gform_force_hooks_js_output', 'allow_gform_scripts_on_page');
Allow Gravity Forms scripts only when a form is present
// Function to check if a Gravity Forms form is present on the page
function allow_gform_scripts_if_form_present() {
global $post;
return (has_shortcode($post->post_content, 'gravityform')) ? true : false;
}
// Prevent Gravity Forms scripts from being added to pages without a form
add_filter('gform_force_hooks_js_output', 'allow_gform_scripts_if_form_present');
Allow Gravity Forms scripts only for logged-in users
// Function to check if the user is logged in
function allow_gform_scripts_for_logged_in_users() {
return (is_user_logged_in()) ? true : false;
}
// Prevent Gravity Forms scripts from being added to pages for non-logged-in users
add_filter('gform_force_hooks_js_output', 'allow_gform_scripts_for_logged_in_users');
Allow Gravity Forms scripts only on specific post types
// Function to check if the current post is a specific post type
function allow_gform_scripts_on_post_type() {
// Replace 'custom_post_type' with the actual post type where you want to allow Gravity Forms scripts
return (get_post_type() == 'custom_post_type') ? true : false;
}
// Prevent Gravity Forms scripts from being added to all pages except those with the specified post type
add_filter('gform_force_hooks_js_output', 'allow_gform_scripts_on_post_type');