How to remove all styles for certain page template?

I know how to create child themes and page templates. I have a child theme of Salient called Salient-Child, and I have a page template called blankPage.php.

For all pages that use this template, I want there to be no CSS loaded at all.

I’m aware of wp_register_script, wp_deregister_script, wp_deregister_style, wp_dequeue_style, etc, but I’m not sure where/how to use them.

I’ve tried typing some of those functions within blankPage.php itself, and I’ve also tried editing functions.php. I’ve tried using a conditional with if(is_page_template('blankPage.php')){...}.

Any guidance would be appreciated.

Thanks!

2 s
2

You can remove specific styles and java scripts for specific page template like below. Add the code into the functions.php file of your current theme. To see the list of all JS and CSS you may want to use a plugin like this: https://wordpress.org/plugins/debug-bar-list-dependencies/

/**
 * Remove specific java scripts.
 */
function se_remove_script() {
    if ( is_page_template( 'blankPage.php' ) ) {
        wp_dequeue_script( 'some-js' );
        wp_dequeue_script( 'some-other-js' );
    }
}

add_action( 'wp_print_scripts', 'se_remove_script', 99 );

/**
 * Remove specific style sheets.
 */
function se_remove_styles() {
    if ( is_page_template( 'blankPage.php' ) ) {
        wp_dequeue_style( 'some-style' );
        wp_dequeue_style( 'some-other-style' );
    }
}

add_action( 'wp_print_styles', 'se_remove_styles', 99 );

You can remove all styles and java scripts in one go for specific page template like below. Add the code into the functions.php file of your current theme.

/**
 * Remove all java scripts.
 */
function se_remove_all_scripts() {
    global $wp_scripts;
    if ( is_page_template( 'blankPage.php' ) ) {
        $wp_scripts->queue = array();
    }
}

add_action( 'wp_print_scripts', 'se_remove_all_scripts', 99 );

/**
 * Remove all style sheets.
 */
function se_remove_all_styles() {
    global $wp_styles;
    if ( is_page_template( 'blankPage.php' ) ) {
        $wp_styles->queue = array();
    }
}

add_action( 'wp_print_styles', 'se_remove_all_styles', 99 );

Leave a Comment