I made a parent theme and a child theme in one of my project. I enqueued CSS and JavaScripts in my parent theme like below:

function project_necessary_scripts() {
    //Stylesheets
    wp_register_style( 'bootstrap-css', get_template_directory_uri() .'/css/bootstrap.min.css' );
    wp_register_style( 'bootstrap-map', get_template_directory_uri() .'/css/bootstrap.css.map' );
    wp_register_style( 'project-css', get_stylesheet_uri() );

    wp_enqueue_style( 'bootstrap-css' );
    wp_enqueue_style( 'bootstrap-map' );
    wp_enqueue_style( 'project-css' );

    //JavaScripts
    wp_register_script( 'modernizr-js', get_template_directory_uri() .'/js/modernizr-2.8.3.min.js', array(), '2.8.3' );
    wp_register_script( 'project-js', get_template_directory_uri() .'/js/project.min.js', array('jquery'), '20150401', true );

    wp_enqueue_script( 'modernizr-js' );
    wp_enqueue_script( 'project-js' );
}
add_action( 'wp_enqueue_scripts', 'project_necessary_scripts' );

Now, in my Child theme I want to dequeue some stylesheets and javascripts. So I used the following code:

function project_dequeue_unnecessary_scripts() {
    wp_dequeue_style( 'bootstrap-map' );
    wp_dequeue_script( 'modernizr-js' );
    wp_dequeue_script( 'project-js' );
}
add_action( 'wp_print_scripts', 'project_dequeue_unnecessary_scripts' );

But actually the bootstrap.css.map file is still enqueuing, but the modernizr-js project-js is not loading, so it's working partially. How can I solve that?

I even tried action priorities:

add_action( 'wp_print_scripts', 'project_dequeue_unnecessary_scripts', 11 );
up vote 18 down vote accepted

You are very nearer to the solution, because you are on the right path. Just to tweak a little bit:

There are two such action hooks:

  1. wp_print_scripts, and
  2. wp_print_styles

So the way you can do it, is: hook 'em differently:

//Dequeue Styles
function project_dequeue_unnecessary_styles() {
    wp_dequeue_style( 'bootstrap-map' );
        wp_deregister_style( 'bootstrap-map' );
}
add_action( 'wp_print_styles', 'project_dequeue_unnecessary_styles' );

//Dequeue JavaScripts
function project_dequeue_unnecessary_scripts() {
    wp_dequeue_script( 'modernizr-js' );
        wp_deregister_script( 'modernizr-js' );
    wp_dequeue_script( 'project-js' );
        wp_deregister_script( 'project-js' );
}
add_action( 'wp_print_scripts', 'project_dequeue_unnecessary_scripts' );

And the correct way is to deregister them beside dequeuing. So first dequeue them, and then deregister them accordingly.

  • according to docs: Since WordPress 3.3, wp_print_scripts should not be used to enqueue styles or scripts. Use wp_enqueue_scripts instead. – user1264304 Feb 5 at 22:12
  • @user1264304 We're not enqueueing things here, we're dequeueing things. – Mayeenul Islam Feb 6 at 4:31
  • yes, but according to link, one should use add_filter( 'wp_enqueue_scripts', 'project_dequeue_unnecessary_scripts') – user1264304 Feb 6 at 20:42

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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