I want to dequeue a script (‘enterprise-responsive-menu’), but the function I have in my template does not do that. Does anything look wrong?

Here is the enqueue in functions.php-

//* Enqueue Scripts 
add_action( 'wp_enqueue_scripts', 'enterprise_load_scripts' );
function enterprise_load_scripts() {

wp_enqueue_script( 'enterprise-responsive-menu', get_bloginfo( 'stylesheet_directory' ) . '/js/responsive-menu.js', array( 'jquery' ), '1.0.0' );

wp_enqueue_style( 'dashicons' );

wp_enqueue_style( 'google-fonts', '//fonts.googleapis.com/css?family=Lato:300,700,300italic|Titillium+Web:600', array(), CHILD_THEME_VERSION ); 
}'

Here is the dequeue code in my template –

//Remove Mobile Header
function project_dequeue_unnecessary_scripts() {
wp_dequeue_script( 'enterprise-responsive-menu' );
wp_deregister_script( 'enterprise-responsive-menu' );
}
add_action( 'wp_print_scripts', 'project_dequeue_unnecessary_scripts' );

2 Answers
2

Move your project_dequeue_unnecessary_scripts() function to your functions.php file and add a conditional statement to determine if the appropriate template is being loaded. E.g.:

// Remove Mobile Header
function project_dequeue_unnecessary_scripts() {
    if ( is_page_template( 'name-of-template.php' ) ) {
        wp_dequeue_script( 'enterprise-responsive-menu' );
        wp_deregister_script( 'enterprise-responsive-menu' );
    }
}
add_action( 'wp_print_scripts', 'project_dequeue_unnecessary_scripts' );

I suspect that your function is not working because it has been placed somewhere after the call to get_header() in the template file which means it would be too late to dequeue the script. Declaring functions in template files is not a good practice anyway, so use your functions.php file or another include.

Leave a Reply

Your email address will not be published. Required fields are marked *