Skip to Content

dequeues()

Dequeue certain scripts when not needed.

PHP April 26, 2017

Usage

This function runs automatically, so it is not called manually. Is this incorrect?

Additional Notes

This dequeues scripts that are not needed on certain pages.

Source File

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

1 Hook

Find these filters and actions in the source code below to hook into them. Use do_action() and add_filter() in your functions file or plugin.

Filters
"pre_nebula_dequeues"
Need a new filter hook? Request one here.

Actions
This function has no action hooks available. Request one?

PHP
        public function dequeues(){
            $override = apply_filters('pre_nebula_dequeues', null);
            if ( isset($override) ){return;}

            if ( !is_admin() ){
                $this->deregister('contact-form-7', 'style'); //Removing CF7 styles in favor of Bootstrap + Nebula
                $this->deregister('wp-embed', 'script'); //WP Core WP-Embed - Override this only if embedding external WordPress posts into this WordPress site. Other oEmbeds are NOT AFFECTED by this!

                //Page specific dequeues
                if ( is_front_page() ){
                    $this->deregister('thickbox', 'style'); //WP Core Thickbox - Override this if thickbox type gallery IS used on the homepage.
                    $this->deregister('thickbox', 'script'); //WP Thickbox - Override this if thickbox type gallery IS used on the homepage.
                }

                //Get the last WordPress action handle that was called (so we know which one we are likely "inside")
                $action_keys = array_keys($GLOBALS['wp_actions']); //Store in a variable first so only the variable is passed to end() as a reference
                $last_action = end($action_keys); //Change to array_key_last($GLOBALS['wp_actions']) when PHP 7.3 is minimum version

                if ( !empty($last_action) ){
                    //Dequeue styles based on selected Nebula options
                    if ( $last_action !== 'wp_print_scripts' ){ //Check the last hook to run and skip dequeuing styles on the print scripts hook
                        $styles_to_dequeue = $this->get_option('dequeue_styles');
                        if ( !empty($styles_to_dequeue) ){
                            $this->check_dequeue_rules($styles_to_dequeue, 'styles');
                        }
                    }

                    //Dequeue scripts based on selected Nebula options
                    if ( $last_action !== 'wp_print_styles' ){ //Check the last hook to run and skip dequeuing scripts on the print styles hook
                        $scripts_to_dequeue = $this->get_option('dequeue_scripts');
                        if ( !empty($scripts_to_dequeue) ){
                            $this->check_dequeue_rules($scripts_to_dequeue, 'scripts');
                        }
                    }
                }
            }
        }

Override

To override this PHP function, use this hook in your child theme or plugin ("my_custom" can be changed):

PHP
add_filter('pre_nebula_dequeues', 'my_custom_dequeues'); 
function my_custom_dequeues(){ 
    //Write your own code here

    return true; //Return true to prevent the original function from running afterwords
}

You can completely disable this PHP function with a single line actions:

PHP
 add_filter('pre_nebula_dequeues', '__return_false');