0

Here's what I am trying to achieve: I have to make a template which does NOT require any of the theme's (main theme's) css and js except for the bootstrap css framework.

My question is this: I'd like to know if this ruleset in my functions.php is valid:

if ( is_page_template( 'specific-template.php' ) ) {
    //  Remove CSS 1
    wp_dequeue_style( 'generic styles 1' );
    //  Remove CSS 2 
    wp_dequeue_style( 'generic styles 2' );
    //  Remove CSS 3
    wp_dequeue_style( 'generic styles 3' );

    //  Remove JS 1
    wp_dequeue_script( 'js 1' );
    //  Remove JS 2
    wp_dequeue_script( 'js 2' );

    // CSS Add Style 1
    wp_enqueue_style( 'css style 1', get_template_directory_uri() . '/css/new-1.css', array(), '1.0');
    // CSS Add Style 2
    wp_enqueue_style( 'css style 2', get_template_directory_uri() . '/css/new-2.css', array(), '1.0');
    // CSS Add Style 3
    wp_enqueue_style( 'css style 3', get_template_directory_uri() . '/css/new-3.css', array(), '1.0');
    // CSS Add Style 4
    wp_enqueue_style( 'css style 4', get_template_directory_uri() . '/css/new-4.css', array(), '1.0');
    // CSS Add Style 5
    wp_enqueue_style( 'css style 5', get_template_directory_uri() . '/css/new-5.css', array(), '1.0');

    // JS Add Style 1
    wp_enqueue_script( 'js style 1', get_template_directory_uri() . '/css/new-1.js', array(), '1.0');
    // JS Add Style 2
    wp_enqueue_script( 'js style 2', get_template_directory_uri() . '/css/new-2.js', array(), '1.0');

}

Is my thinking way off or is the above standard WP practice when it comes to creating a template which is dependent on a selection of other CSS/ JS files?

Thanks for all direction

0

Using wp_enqueue_scripts

You should use a proper hook to enqueue/dequeue your scripts, to ensure that it works flawlessly. A proper plugin/theme uses wp_enqueue_scripts to do this, so you should do the same. Wrap your code inside a function, and bind it to this action hook. Also, remember to set the priority to something that makes sure it will override the others.

// Setting the priority to 999 to make sure it runs
// after every other script is enqueued
add_action('wp_enqueue_scripts', 'enqueue_my_scripts', 999);
enqueue_my_scripts () {
    if ( is_page_template( 'specific-template.php' ) ) {
        // Your code here
    }
}
  • ok thanks! So basically is it ok to list all 10 scripts (js/ css) in the same hook? – henry Aug 28 '17 at 13:52

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.