• Resolved vallered

    (@vallered)


    As your plugin uses JQuery, I want your assets only to be loaded on WC pages for performance reasons. Can you provide a filter I can use for that purpose?

    The page I need help with: [log in to see the link]

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author stepasyuk

    (@stepasyuk)

    Hello @vallered

    Thank you for using Filter Everything plugin.

    Assets are included in common WordPress way through wp_print_styles and wp_print_scripts hooks and you can disable them with these hooks. Also you can check if the current page is WooCommerce page with the function is_woocommerce()

    I hope this will be helpful for you.


    If it is possible I would be thankful for a positive review about the plugin here on www.ads-software.com – https://www.ads-software.com/support/plugin/filter-everything/reviews/ This will help me to populate the plugin.

    Thank you in advance!

    Thread Starter vallered

    (@vallered)

    Maybe you can save me some hours in sorting out how to implement that…I am currently using the following snippet to unload a bunch of assets from non WooCommerce pages, maybe you can tell me what to add, or, how a separate snippet should look like, that would be great:

    /**

    • Callback function that returns true if the current page is a WooCommerce page or false if otherwise.
      *
    • @return boolean true for WC pages and false for non WC pages
      */
      function is_wc_page() {
      return class_exists( ‘WooCommerce’ ) && ( is_woocommerce() || is_cart() || is_checkout() || is_account_page() );
      }

    add_action( ‘template_redirect’, ‘conditionally_remove_wc_assets’ );
    /**

    • Remove WC stuff on non WC pages.
      */
      function conditionally_remove_wc_assets() {
      // if this is a WC page, abort.
      if ( is_wc_page() ) {
      return;
      } // remove WC generator tag
      remove_filter( ‘get_the_generator_html’, ‘wc_generator_tag’, 10, 2 );
      remove_filter( ‘get_the_generator_xhtml’, ‘wc_generator_tag’, 10, 2 ); // unload WC scripts
      remove_action( ‘wp_enqueue_scripts’, [ WC_Frontend_Scripts::class, ‘load_scripts’ ] );
      remove_action( ‘wp_print_scripts’, [ WC_Frontend_Scripts::class, ‘localize_printed_scripts’ ], 5 );
      remove_action( ‘wp_print_footer_scripts’, [ WC_Frontend_Scripts::class, ‘localize_printed_scripts’ ], 5 ); // remove “Show the gallery if JS is disabled”
      remove_action( ‘wp_head’, ‘wc_gallery_noscript’ ); // remove WC body class
      remove_filter( ‘body_class’, ‘wc_body_class’ );
      }
      /**
    • only load German Market Assets on WC pages
      *
    • */
      add_filter( ‘gm_include_frotend_js_and_css_only_for_wc_content’, ‘__return_true’ );

    add_filter( ‘woocommerce_enqueue_styles’, ‘conditionally_woocommerce_enqueue_styles’ );
    /**

    • Unload WC stylesheets on non WC pages
      *
    • @param array $enqueue_styles
      */
      function conditionally_woocommerce_enqueue_styles( $enqueue_styles ) {
      return is_wc_page() ? $enqueue_styles : array();
      }

    add_action( ‘wp_enqueue_scripts’, ‘conditionally_wp_enqueue_scripts’ );
    /**

    • Remove inline style on non WC pages
      */
      function conditionally_wp_enqueue_scripts() {
      if ( ! is_wc_page() ) {
      wp_dequeue_style( ‘woocommerce-inline’ );
      }
      }

    // add_action( ‘init’, ‘remove_wc_custom_action’ );
    function remove_wc_custom_action(){
    remove_action( ‘wp_head’, ‘wc_gallery_noscript’ );
    }

    Plugin Author stepasyuk

    (@stepasyuk)

    Hello @vallered

    Thank you for your feedback. I just saw this your comment.

    To remove extra JS from Filter Everything on non-Woocommerce pages in your case please use this code

    if ( function_exists( 'is_wc_page' ) ) {
        function wpc_remove_fe_scripts() {
            global $wp_scripts;
    
            if ( ! is_wc_page() ){
                foreach( $wp_scripts->queue as $k => $script_handle ){
                    if( $script_handle === 'wpc-filter-everything' ){
                        unset($wp_scripts->queue[$k]);
                    }
    
                    if( $script_handle === 'wc-jquery-ui-touchpunch' ){
                        unset($wp_scripts->queue[$k]);
                    }
                }
            }
        }
    
        add_action( 'wp_print_scripts', 'wpc_remove_fe_scripts', 999 );
    }
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Load Assets only on WC Pages’ is closed to new replies.