Skip to Content
Menu

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.

Was this page helpful? Yes No


    A feedback message is required to submit this form.


    Please check that you have entered a valid email address.

    Enter your email address if you would like a response.

    Thank you for your feedback!

    Source File

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

    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() ){
                    $current_action = current_action(); //Get the current WordPress action handle that was called (so we know which one we are "inside")
                    $timer_name = $this->timer('Advanced Dequeues (' . $current_action . ')');
                    $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.
                    }
    
                    if ( !empty($current_action) ){
                        //Ignore script dependencies on style-based hooks (enqueue_scripts and print_scripts)
                        // if ( strpos($current_action, 'scripts') !== false ){
                        //    //Disabled because some functionality still needs wp-polyfill even in modern browsers. Ugh. Ex: https://nebula.gearside.com/functions/infinite_load_query/
                        //     //Remove "wp-polyfill" but first need to remove that dependency from other scripts. In the future, this may no longer be needed... hopefully. Watch this issue: https://github.com/WordPress/gutenberg/issues/21616
                        //     $scripts = wp_scripts(); //Get all of the script dependencies
                        //     foreach ( $scripts->registered as $registered_script ){ //Loop through all registered scripts
                        //         foreach ( $registered_script->deps as $dep_key => $handle ){ //Loop through each of the dependencies
                        //             if ( $handle === 'wp-polyfill' ){ //If this dependency is "wp-polyfill"
                        //                 unset($registered_script->deps[$dep_key]); //Remove this dependency
                        //             }
                        //         }
                        //     }
                        //     $this->deregister('wp-polyfill', 'script', false); //Now we can deregister "wp-polyfill" without breaking other assets
                        // }
    
                        //Dequeue styles based on selected Nebula options
                        if ( $current_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 ( $current_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');
                            }
                        }
                    }
    
                    $this->timer($timer_name, 'end');
                }
            }
    

    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');