Roots Discourse

Dequeuing Gravity Forms


#1

I’ve already dequeued a few plugins like the Roots Share Buttons with no issues. I’m just wondering about Gravity forms, I use it with most client projects, but haven’t dequeued it in the past; Would it be a good idea to dequeue GF or do you think that might cause problems anyway?

I haven’t had much success in testing it because when tried to dequeue the scripts it just didn’t work, unfortunately. I used Query Monitor to find the dependency names, which are:

  • gform_conditional_logic
  • gform_gravityforms
  • gform_json

From my extras.php file

function remove_plugin_assets() {
  // Roots Share Buttons
  wp_dequeue_style('roots-share-buttons');
  wp_dequeue_script('roots-share-buttons');
  // UK Cookie Consent
  wp_dequeue_script('cookie-consent');
  // Gravity Forms
  wp_dequeue_script('gform_conditional_logic');
  wp_dequeue_script('gform_gravityforms');
  wp_dequeue_script('gform_json');
}
add_action('wp_enqueue_scripts', __NAMESPACE__ . '\\remove_plugin_assets');

If anyone else had been able to dequeue Gravity forms without issues, I’d be interested to know how you did it and where I’m going wrong.

Thank you :smile:


#2

I found the following scripts and styles enqueued inside form_display.php

		    wp_enqueue_style( 'gforms_reset_css', GFCommon::get_base_url() . "/css/formreset{$min}.css", null, GFCommon::$version );

    			if ( self::has_datepicker_field( $form ) ) {
    				wp_enqueue_style( 'gforms_datepicker_css', GFCommon::get_base_url() . "/css/datepicker{$min}.css", null, GFCommon::$version );
    			}

    			wp_enqueue_style( 'gforms_formsmain_css', GFCommon::get_base_url() . "/css/formsmain{$min}.css", null, GFCommon::$version );
    			wp_enqueue_style( 'gforms_ready_class_css', GFCommon::get_base_url() . "/css/readyclass{$min}.css", null, GFCommon::$version );
    			wp_enqueue_style( 'gforms_browsers_css', GFCommon::get_base_url() . "/css/browsers{$min}.css", null, GFCommon::$version );

    			if ( is_rtl() ) {
    				wp_enqueue_style( 'gforms_rtl_css', GFCommon::get_base_url() . "/css/rtl{$min}.css", null, GFCommon::$version );
    			}
    		}

    		if ( self::has_conditional_logic( $form ) ) {
    			wp_enqueue_script( 'gform_conditional_logic' );
if ( self::has_datepicker_field( $form ) ) {
			wp_enqueue_script( 'gform_datepicker_init' );
		}

		if ( $ajax || self::has_price_field( $form ) || self::has_password_strength( $form ) || GFCommon::has_list_field( $form ) || GFCommon::has_credit_card_field( $form ) || self::has_conditional_logic( $form ) || self::has_currency_format_number_field( $form ) || self::has_calculation_field( $form ) || self::has_recaptcha_field( $form ) ) {
			wp_enqueue_script( 'gform_gravityforms' );
		}

		if ( GFCommon::has_multifile_fileupload_field( $form ) ) {
			wp_enqueue_script( 'plupload-all' );
		}

		if ( self::has_fileupload_field( $form ) ) {
			wp_enqueue_script( 'gform_gravityforms' );
			GFCommon::localize_gform_gravityforms_multifile();
		}

		if ( self::has_enhanced_dropdown( $form ) || self::has_pages( $form ) ) {
			wp_enqueue_script( 'gform_json' );
			wp_enqueue_script( 'gform_gravityforms' );
		}

		if ( self::has_character_counter( $form ) ) {
			wp_enqueue_script( 'gform_textarea_counter' );
		}

		if ( self::has_input_mask( $form ) ) {
			wp_enqueue_script( 'gform_masked_input' );
		}

		if ( self::has_enhanced_dropdown( $form ) && ! wp_script_is( 'chosen' ) ) {
			wp_enqueue_script( 'gform_chosen' );
		}

		if ( self::has_enhanced_dropdown( $form ) ) {
			if ( wp_script_is( 'chosen', 'registered' ) ) {
				wp_enqueue_script( 'chosen' );
			} else {
				wp_enqueue_script( 'gform_chosen' );
			}
		}

		if ( self::has_placeholder( $form ) ) {
			wp_enqueue_script( 'gform_placeholder' );
		}

So we have the following styles:

  • gforms_reset_css
  • gforms_datepicker_css
  • gforms_formsmain_css
  • gforms_ready_class_css
  • gforms_browsers_css
  • gforms_rtl_css

and the following scripts:

  • gform_conditional_logic
  • gform_datepicker_init
  • gform_gravityforms
  • plupload
  • gform_json
  • gform_textarea_counter
  • gform_masked_input
  • gform_chosen
  • gform_placeholder

Dequeueing stylesheets is explained at Gravity Help and is:

add_action( 'gform_enqueue_scripts_1',  __NAMESPACE__ .  '\\dequeue_gf_stylesheets', 11 );
function dequeue_gf_stylesheets() {
    wp_dequeue_style( 'gforms_reset_css' );
    wp_dequeue_style( 'gforms_datepicker_css' );
    wp_dequeue_style( 'gforms_formsmain_css' );
    wp_dequeue_style( 'gforms_ready_class_css' );
    wp_dequeue_style( 'gforms_browsers_css' );
}

Dequeueing scripts could be:

 add_action( 'gform_enqueue_scripts_2',  __NAMESPACE__ .  '\\dequeue_gf_scripts', 11 );
 function dequeue_gf_scripts() {
   wp_dequeue_script( 'gform_conditional_logic' );
   wp_dequeue_script( 'gform_datepicker_init' );
   wp_dequeue_script( 'gform_gravityforms' );
   wp_dequeue_script( 'gform_json' );
}

#3

They did not work out of the box like this though. CSS can be turned off in Gravity forms:


So I did that for the CSS. And then you can add them to your own queue with:

"main.css": {
      "files": [
        "styles/main.scss",
        "../../../plugins/gravityforms/css/formsmain.min.css",
        "../../../plugins/gravityforms/css/browsers.min.css",
        "../../../plugins/gravityforms/css/forms.min.css",
        "../../../plugins/gravityforms/css/readyclass.min.css",
        "../../../plugins/gravityforms/css/reset.min.css",
        "../../../plugins/gravityforms/css/rtl.min.css"
      ],
      "main": true
    },

Looking into another way and into dequeueing scripts.


#4

Gravity forms won’t enqueue it’s scripts if jQuery isn’t enqueued. In my case I am already deregistering jQuery within setup.php and including it in my main.js file. So for me, because I have wp_deregister_script('jquery'); in setup.php all of Gravity Forms scripts are automatically dequeued.

There’s also a conflict with browserSync, everything resolves when viewing and submitting the form on production.