1

I'm trying to optimize my website, get rid off unwanted scripts and styles. I wanted to ask what is the best way to do so. What is the difference between wp_deregister_script and wp_dequeue_script ? If I want to kick out some scripts and styles only from specific pages, and minify it with w3 total catches, won't it load anyway in one, combined, minified file?

EDIT

I'm trying to dequeue some js and styles with

add_action( 'wp_enqueue_scripts', 'my_dequeue_styles', PHP_INT_MAX );

function my_dequeue_styles() {
    if ( is_front_page() ) {
wp_dequeue_script( 'somescript');
wp_dequeue_style( 'somecss');
    }
}

But it doesn't seem to be working. is_front_page() is only one of conditions tht I tried to use. I placed the code at the begging of functions.php.

  • this is two questions in one, and the second one is about a plugin which makes it off-topic – Mark Kaplun Nov 6 '16 at 8:04
4

Query Monitor will show you what is loaded or you can print them directly.

function print_wp_scripts_queue(){

    $scripts = wp_scripts();

    echo "<pre><h1>scripts</h1>";
    print_r ( $scripts->queue );


    echo "<h1>todo</h1>";
    print_r ( $scripts->to_do );

    echo "<h1>done</h1>";
    print_r ( $scripts->done );

    echo "<h1>registered</h1>";    
    print_r ( $scripts->registered );

    echo "</pre>";
    wp_die();
}

add_action( 'wp_head', 'print_wp_scripts_queue', PHP_INT_MAX );

wp_deregister_script removes a script from the register. Meaning you won't be able to reference by a name later.

wp_dequeue_script will remove it from the queue so it won't render, which is what you want. Just make sure you do it after it has been enqueued.

Some scripts loading are dependent on other scripts to be loaded. So if you dequeue jQuery then most things won't load because that's usually the safest file to wait for. Dependencies also help with making sure styles or scripts have been added so the following style or script can expect the resource to exist.


  • I tried to dequeue some scripts and styles but wp_dequeue_script() and wp_deregister_script() is for some reason not working for me - see my edit – Rafal Nov 6 '16 at 10:58
  • "for some reason not working for me" -- see every question on stackexchange... – jgraup Nov 6 '16 at 12:31

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.