Skip to Content

deregister()

Dequeue and deregister an asset (style or script) and log it with Nebula.

PHP September 11, 2020

Usage

PHP
nebula()->deregister($handle, $type);

Parameters

$handle
(Required) (String) The handle of the asset to deregister
Default: None

$type
(Required) (String) Whether this asset is a "style" or "script"
Default: None

Request or provide clarification »

Examples

PHP
add_action('wp_enqueue_scripts', function(){
    if ( is_front_page() ){
        nebula()->deregister('contact-form-7', 'style');
        nebula()->deregister('contact-form-7', 'script');
    }
}, 3000);

Additional Notes

Common WordPress hooks for deregistering assets include wp_enqueue_scripts, wp_print_styles, and wp_print_scripts. Make sure your hook has a higher priority number so it runs after the original.

This function both dequeues and deregisters the asset. Then it logs it in Nebula to appear in the WordPress admin bar  (under the Edit Post menu).

Nebula will only show the log when the asset is actually enqueued on a page (otherwise the deregistering has no affect on the page), however, it will still attempt to dequeue and deregister it regardless.

How to find plugins’ asset handles

Use the Nebula search tool in the WordPress Admin Dashboard on the Developer Information metabox to find plugin asset handles. Do a search within an individual plugin for “register_” or “enqueue_” to see what/when they are called. You could also loop through the global $wp_scripts->queue and $wp_styles->queue to get a list on an individual page.

Here are some more in-depth instructions/tips for finding and deregistering assets and how Nebula helps.

Source File

Located in /libs/Optimization.php on line 667.

No Hooks

This function does not have any filters or actions available. Request one?
PHP
        public function deregister($handle, $type){
            if ( !empty($handle) ){
                //Styles
                if ( strpos(strtolower($type), 'style') !== false || strpos(strtolower($type), 'css') !== false ){
                    //Check if this style was enqueued
                    if ( wp_style_is($handle, 'enqueued') ){
                        $this->deregistered_assets['styles'][] = $handle; //Add it to the array to log in the admin bar
                    }

                    //Deregister the style either way
                    wp_dequeue_style($handle);
                    wp_deregister_style($handle);
                    return true;
                }

                //Scripts
                //Check if this script was enqueued (and show note if so)
                if ( wp_script_is($handle, 'enqueued') ){
                    $this->deregistered_assets['scripts'][] = $handle;
                }

                //Deregister the script either way
                wp_dequeue_script($handle);
                wp_deregister_script($handle);
                return true;
            }
        }

Override

This function can not be short-circuited with an override filter. Request one?