Skip to content

Instantly share code, notes, and snippets.

@fwazeter
Last active October 24, 2021 23:57
Show Gist options
  • Save fwazeter/cba344b4a7c785b47766c300fc002bed to your computer and use it in GitHub Desktop.
Save fwazeter/cba344b4a7c785b47766c300fc002bed to your computer and use it in GitHub Desktop.
WordPress discover location of scripts & styles from core or plugins to dequeue.
/**
* Dequeue JavaScript or Stylesheet.
*/
function my_dequeue_script()
{
// Run the dequeue script with the handle of the JavaScript file
wp_dequeue_script( $handle );
// Run the dequeue style with the handle of the CSS file
wp_dequeue_style( $handle );
}
add_action( 'wp_enqueue_scripts', 'my_dequeue_script', 100 );

Discover Handles

We need to look inside the wp_styles() object and wp_scripts() object (for js) to discover what's registered & queued. The value of the ["handle"] key will be the the name you input into the $handle var in dequeueing styles/scripts.

function discover_scripts() 
{
    // Registered styles
    var_dump(wp_styles()->registered);

    // Queued styles
    var_dump(wp_styles()->queue);

    // Registered scripts
    var_dump(wp_scripts()->registered);

    // Queued scripts
    var_dump(wp_scripts()->queue);
}
add_action( 'wp_enqueue_scripts', 'discover_scripts', 100 );

Quick Search Style Objects

Refresh any page and you'll get a long list of the object listed out with many entries that start like:

object(_WP_Dependency)#1018 (8) { ["handle"]=> string(8) "thickbox" ["src"]=> string(37) "/wp-includes/js/thickbox/thickbox.css"....

Easy search: CTRL+F "_WP_Dependency" or ["handle"] the ["handle"] value is the key that you'll pass to the dequeue script.

Dequeue Script Code:

/**
 * Dequeue JavaScript or Stylesheet.
 */
function my_dequeue_script() 
{
    // Run the dequeue script with the handle of the JavaScript file
    wp_dequeue_script( $handle );

    // Run the dequeue style with the handle of the CSS file
    wp_dequeue_style( $handle );
}
add_action( 'wp_enqueue_scripts', 'my_dequeue_script', 100 );

Original Reference

function discover_scripts()
{
// Registered styles
var_dump(wp_styles()->registered);
// Queued styles
var_dump(wp_styles()->queue);
// Registered scripts
var_dump(wp_scripts()->registered);
// Queued scripts
var_dump(wp_scripts()->queue);
}
add_action( 'wp_enqueue_scripts', 'discover_scripts', 100 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment