4

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!

8

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 );
  • Your se_remove_all_styles seems right on track for what I'm hoping for, but when I just tried it, it removed all styles from ALL pages, regardless of template. I don't know why, given that there is an "If" statement. Any ideas on how to debug why this would be happening for me? Thanks! – Ryan Oct 11 '15 at 22:40
  • Are you providing correct path for the template file inside the if checking ? – Subharanjan Oct 12 '15 at 1:53
1

I seemed to solve the problem, and it was so simple that I'm shocked I've never seen this mentioned by anyone before.

First, I deleted my entire child theme so that I could start over from a fresh place.

Then, I used https://wordpress.org/plugins/one-click-child-theme/ to create a brand new child theme of Salient.

Then, in the Appearance > Editor menu, I viewed the functions.php file and noticed that it had pregenerated add_action( 'wp_enqueue_scripts', 'theme_enqueue_styles' ); and function theme_enqueue_styles() for me.

So I simply wrapped the contents of the function within if ( !is_page_template( 'rawHtmlPage.php' ) ) { ...}.

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.