I wanted to remove ALL javascript and css from the head, but still call the wp_head function since it loads other stuff (meta, plugins, etc). Most advice tells you to use wp_deregister_script() and wp_deregister_style() to individually remove each file, but you need to know the name they were registered as, which requires you to dig deep into code. I just wanted something that removed all of them, cleared it out. I couldn’t find a function that did this, so I made a workaround.

This is not supported by WordPress, so it may not work in the future, but works as of now. Also this only works if the css and js were added the correct way (via wp_enqueue_script() and wp_enqueue_style()) – if the styles and scripts are hardcoded in the theme, you’ll have to edit those theme files directly.

    global $wp_scripts, $wp_styles;
    $wp_scripts = new WP_Scripts();
    $wp_styles = new WP_Styles();
               
    wp_head();

Hope it helps you!

About Shane
Shane is the creator of Page Speed Analytics tool MachMetrics. You can follow his entrepreneurial activities on his blog SideProjectProfit

  3 Responses to “WordPress: Removing all Javascript and Styles/CSS from the head”

  1. Thanks, I was looking everywhere for a way to remove the standard style.css stylesheet. Several posts claimed it could not be removed. Nice work!

  2. Do you know if this works with the latest WordPress 3.9? Would this code be placed in a theme’s function.php? And if I want it only executing for a specific page, would this work?


    if ( is_page( # ) ) {
    global $wp_scripts, $wp_styles;
    $wp_scripts = new WP_Scripts();
    $wp_styles = new WP_Styles();
    wp_head();
    }

    Thank you!

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

(required)

(required)

   
© 2012 ShaneLabs Suffusion theme by Sayontan Sinha