0

I have a page template, which is working nicely. But when I include , it loads the themes CSS and js files, which breaks my page template.

However, I still need the wp_head for some plugins to work.

Is there a way to stop wp_head from loading any of my theme's CSS & js files?

I want the page template to only use its own CSS + js.

  • what template are you using? did you added any scripts to enqued styles in functions.php? – elicohenator Nov 17 '16 at 19:15
3

Thank you for your help! This solution works with my theme:

function remove_all_theme_styles() {
if ( is_page_template('template-landing.php') ) {
    global $wp_styles;
    $wp_styles->queue = array();
}
}
add_action('wp_print_styles', 'remove_all_theme_styles', 100);
0

There is no clear way to reliably do that (sans hacky approaches like buffering page output).

Assuming that everything only ever uses proper WP APIs to queue you could de–queue all resources. A little more harsh way would be to unhook wp_print_styles(), wp_print_scripts(), and wp_print_footer_scrtipts().

In a nutshell there is no easy way to do this thoroughly and cleanly. You either do something drastic like removing head/footer hooks altogether or you have to micromanage it script by script.

0

It depends on if the theme is using enqueued scripts/styles or if they're hard coded in the header.

If the CSS & JS are enqueued, you can dequeue them in the specified template:

function wpse_246547_deregister() {
    if ( is_page_template('YOUR-TEMPLATE.php') ) {
        wp_dequeue_script( 'name-of-script' );
        wp_deregister_script( 'name-of-script' );
        wp_dequeue_style( 'name-of-stylesheet' );
        wp_deregister_style( 'name-of-stylesheet' );
    }
}
add_action( 'wp_enqueue_scripts', 'wpse_246547_deregister', 100 );

https://codex.wordpress.org/Function_Reference/wp_dequeue_script https://codex.wordpress.org/Function_Reference/wp_dequeue_style

If the CSS & JS references are hard-coded in the header.php file, you can specify in your template to use a different header file (in this example, it would look for header-alt.php):

get_header( 'alt' );

https://codex.wordpress.org/Function_Reference/get_header#Multiple_Headers

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.