Skip Navigation

[Resolved] Dequeue unused styles and scripts

This thread is resolved. Here is a description of the problem and solution.

Problem: I would like to dequeue the stylesheets and script files enqueued for the lightbox plugin, but the script I added does not seem to dequeue these assets as expected.

Solution: Use priority 1 in the third param of add_action to dequeue these assets more effectively:

add_action( 'wp_print_footer_scripts', 'ts_dequeue_scripts', 1 );
function ts_dequeue_scripts( $handles ){
   
    wp_dequeue_script( 'lightbox' );
}
add_action( 'wp_print_footer_scripts', 'ts_dequeue_scripts', 1 );
function ts_dequeue_scripts( $handles ){
   
    wp_dequeue_script( 'lightbox' );
}

Relevant Documentation:
https://developer.wordpress.org/reference/functions/add_action/

This support ticket is created 2 years, 9 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 2 replies, has 2 voices.

Last updated by Paul Marconi 2 years, 9 months ago.

Assisted by: Christian Cox.

Author
Posts
#2103235

Hi there,

I would like to dequeue some unused styles and scripts from the toolset view plugin, but it's not working.
I am trying to remove these 2 lightbox files, a script and a css file:

<script type='text/javascript' src='XXX/wp-content/plugins/wp-views/vendor/toolset/blocks/public/vendor/lightbox/js/lightbox.min.js?ver=2.10.0' id='lightbox-js'></script>

<link rel='stylesheet' id='lightbox-css' href='XXX/wp-content/plugins/wp-views/vendor/toolset/blocks/public/vendor/lightbox/css/lightbox.min.css?ver=2.10.0' type='text/css' media='all' />

I added the code below to my functions.php file, but it's not working:
function cleanup_scripts( $handles) {
wp_dequeue_script( "lightbox" );
}
add_action( 'wp_print_scripts', 'cleanup_scripts', 9999 );

function cleanup_styles() {
wp_dequeue_style( 'lightbox' );
}
add_action( 'wp_print_styles', 'cleanup_styles', 100 );

How could we dequeue those 2 unused files?

Thank you!

#2103371

Hello, I've seen other Users have luck with similar code using priority 1 so I would try that first:

add_action( 'wp_print_footer_scripts', 'ts_dequeue_scripts', 1 );
function ts_dequeue_scripts( $handles ){
  
    wp_dequeue_script( 'lightbox' );
}
add_action( 'wp_print_footer_scripts', 'ts_dequeue_scripts', 1 );
function ts_dequeue_scripts( $handles ){
  
    wp_dequeue_script( 'lightbox' );
}

If that is not working on your site, it may depend on several other factors like:
- Is your Views plugin up-to-date? Several asset loading optimizations were introduced as recently as Views 3.5, so updating to the latest version of all Toolset plugins should improve overall site load times and the number of assets required for any post.
- Where do you see these files enqueued on the front-end of your site? Is it just one specific page, or in all posts of a specific post type, an archive of some kind, etc.?
- The site's theme or other plugins may also enqueue the lightbox component in a way that reuses the lightbox assets from the Views / Blocks plugin. What happens if you activate a default theme like Twenty Twenty One and deactivate all plugins except Types and Views (or Blocks) - are the lightbox assets still loaded?

#2108497

My issue is resolved now. Thank you!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.