How to Remove Gravity Forms CSS style-sheets and Scripts from Specific Pages
Gravity forms has a nice option in the settings area to remove it’s css output from all pages. However when you want to remove the style-sheets from individual pages it gets more complicated.
Here’s a simple function which will remove Gravity Forms CSS and selected scripts from the front_page:
Cut and paste this code into your functions.php file.
//Deregister Gravity Stylesheets and Scripts from specific pages
add_action("gform_enqueue_scripts", "deregister_scripts");
function deregister_scripts(){
//Change this conditional to target whatever page or form you need.
if(is_front_page()) {
//These are the CSS stylesheets
wp_deregister_style("gforms_formsmain_css");
wp_deregister_style("gforms_reset_css");
wp_deregister_style("gforms_ready_class_css");
wp_deregister_style("gforms_browsers_css");
//These are the scripts.
//NOTE: Gravity forms automatically includes only the scripts it needs, so be careful here.
//wp_deregister_script("gforms_conditional_logic_lib");
//wp_deregister_script("gforms_ui_datepicker");
//wp_deregister_script("gforms_gravityforms");
//wp_deregister_script("gforms_character_counter");
//wp_deregister_script("gforms_json");
//wp_deregister_script("jquery");
}
}
Aaron, I was going nuts trying to find a function that would remove gform css from WP pages. It appears yours is the only up to date advice on this subject. Thanks much! 🙂
No problem, glad it helped!
Thanks so much- this was great.
Can you help me for removing styles from a form id?
So far I wrote the following but it’s not working..
add_action(“gform_enqueue_scripts”, “gform_deregister_scripts”);
function gform_deregister_scripts(){
if(‘/[gravityform(.*?)id=7(.*?)]/’) {
wp_deregister_style(“gforms_formsmain_css”);
}
}
I think you were really close with your code to remove gravity forms by the form ID.
It looks like you are using a regex but are missing the preg_match… try something like this: (untested)
add_action(“gform_enqueue_scripts”, “remove_gravityforms_by_id”);
function remove_gravityforms_by_id() {
global $post;
if(preg_match('/[gravityform(.*?)id=7(.*?)]/', $post->post_content, $matches)) {
wp_deregister_style('gforms_css');
//or any of the the other gforms scripts and styles.
}
}