2

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' );
4
  • Why are you deregistering the script when you can simply not register it to begin with? I sense there is missing information here, please provide more context about where the code is located, including which file your dequeue code is located in – Tom J Nowell Sep 8 '16 at 0:51
  • @Tom, The script is enqueued in functions.php, meaning it is global and applied to every template. I don't want to remove it globally, but I need to deactivate it on a specific page template. Does this clarify my question a little better? goto10 provided a solution that I believe will work. I'm going to try implementing tomorrow. – Carter Steinhoff Sep 8 '16 at 1:01
  • So your dequeue scripts hook is not in functions.php? It's in a page template? Where in the file is it? The timing/when it's added is important – Tom J Nowell Sep 8 '16 at 13:03
  • Ya, I was vaguely aware the importance of placement, but I thought I had it in the right spot. However, I have found out I only need to run the script I wanted to dequeue on one page template, so my best bet is to just place a conditional statement in the functions.php that only enqueues the script on a specific page template. – Carter Steinhoff Sep 8 '16 at 16:31
3

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.

1
  • 1
    Thanks Dave! This was exactly the help that I needed. Also, very quick to answer. – Carter Steinhoff Sep 8 '16 at 16:33
0
add_action( 'wp_enqueue_scripts', 'enterprise_load_scripts' );
function enterprise_load_scripts(){
    if ( !is_page_template( 'name-of-template.php' ) ) {
        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 ); 
}

Instead of dequeue, what if you put that specific script in a condition so that it loads only on pages with any template except those where you don't want to..

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.