2

I have this weird problem. Trying to enqueue a CSS file on an existing functions.php file and nothing seems to work. This is the pertinent part of the functions file, made by someone else based on TwentySixteen theme :

function twentysixteen_scripts() {

    // Add custom fonts, used in the main stylesheet.

    wp_enqueue_style( 'twentysixteen-fonts', twentysixteen_fonts_url(), array(), null );

    // Add Genericons, used in the main stylesheet.
    wp_enqueue_style( 'genericons', get_template_directory_uri() . '/genericons/genericons.css', array(), '3.4.1' );
    wp_enqueue_style( 'bootstrap', get_template_directory_uri() . '/css/bootstrap.css', array(), '3.4.1' );
    wp_enqueue_style( 'theme-owl', get_template_directory_uri() . '/css/owl.carousel.css', array(), '3.4.1' );
    wp_enqueue_style( 'theme-owl-min', get_template_directory_uri() . '/css/owl.theme.css', array(), '3.4.1' );



    // Theme stylesheet.

    wp_enqueue_style( 'twentysixteen-style', get_stylesheet_uri() );
     if (! is_front_page() || is_single())
    {wp_enqueue_style( 'blog-styles', get_template_directory_uri() . 'full-style.css', array( 'twentysixteen-style' ), '20170410' );}
    // Load the Internet Explorer specific stylesheet.
    wp_enqueue_style( 'twentysixteen-ie', get_template_directory_uri() . '/css/ie.css', array( 'twentysixteen-style' ), '20150930' );
    /* 09.02.2017 */
    wp_enqueue_style( 'jquery-ui', get_template_directory_uri() . '/css/jquery-ui.css', array( 'twentysixteen-style' ), '20150930' );

    /* 09.02.2017 */
    wp_style_add_data( 'twentysixteen-ie', 'conditional', 'lt IE 10' );
    // Load the Internet Explorer 8 specific stylesheet.

    wp_enqueue_style( 'twentysixteen-ie8', get_template_directory_uri() . '/css/ie8.css', array( 'twentysixteen-style' ), '20151230' );
    wp_style_add_data( 'twentysixteen-ie8', 'conditional', 'lt IE 9' );
    // Load the Internet Explorer 7 specific stylesheet.
    wp_enqueue_style( 'twentysixteen-ie7', get_template_directory_uri() . '/css/ie7.css', array( 'twentysixteen-style' ), '20150930' );
    wp_style_add_data( 'twentysixteen-ie7', 'conditional', 'lt IE 8' );
    // Load the html5 shiv.
    wp_enqueue_script( 'twentysixteen-html5', get_template_directory_uri() . '/js/html5.js', array(), '3.7.3' );
    wp_script_add_data( 'twentysixteen-html5', 'conditional', 'lt IE 9' );
    wp_enqueue_script( 'twentysixteen-skip-link-focus-fix', get_template_directory_uri() . '/js/skip-link-focus-fix.js', array(), '20151112', true );
    /* 09.02.2017 start */
    wp_enqueue_script( 'jquery-ui', get_template_directory_uri() . '/js/jquery-ui.js', array(), '20151112', true );
    //wp_enqueue_script( 'taskCompRegress', get_template_directory_uri() . '/js/taskCompRegress.js', array(), '20151112', true );
    if(is_single(556) || is_single(274)){
    wp_enqueue_script( 'UXSalary2014', get_template_directory_uri() . '/js/UXSalary2014.js', array(), '20151112', true );
    }
    if(is_single(6440)){
    wp_enqueue_script( 'UXSalary2016', get_template_directory_uri() . '/js/UXSalary2016.js', array(), '20170314', true );
    }
    if(is_single(3656)){
        wp_enqueue_script( 'UXSalary.js', get_template_directory_uri() . '/js/UXSalary.js', array(), '20110823', true );
    }

    if(is_single(204)){
        wp_enqueue_script( 'taskCompRegress.js', get_template_directory_uri() . '/js/taskCompRegress.js', array(), '20110321', true );
    }


    if(is_single(274)){
    //wp_enqueue_script( 'UXSalary', get_template_directory_uri() . '/js/UXSalary.js', array(), '20151112', true );
        wp_enqueue_script( 'npsSUSMeanRegress', get_template_directory_uri() . '/js/npsSUSMeanRegress.js', array(), '20151112', true );
    }
    if(is_single(458)){
        wp_enqueue_script( 'UXQuiz', get_template_directory_uri() . '/js/UXQuiz.js', array(), '20131112', true );
    }
    //wp_enqueue_script( 'UXQuiz', get_template_directory_uri() . '/js/UXQuiz.js', array(), '20151112', true );
    if(is_single(3695)){
    wp_enqueue_script( 'bs', get_template_directory_uri() . '/js/bs.js', array(), '20151112', true );
    }
    if(is_single(3703)){
    wp_enqueue_script( 'uiProbs', get_template_directory_uri() . '/js/uiProbs.js', array(), '20151112', true );
     }
    wp_enqueue_script( 'ciquiz', get_template_directory_uri() . '/js/ciquiz.js', array(), '20151112', true );
    wp_enqueue_script( 'geoMean', get_template_directory_uri() . '/js/geoMean.js', array(), '20151112', true );
    wp_enqueue_script( 'jquery-2.2.4.min', get_template_directory_uri() . '/js/jquery-2.2.4.min.js', array(), '20151112', true );
    wp_enqueue_script( 'jquery-3.1.0.min.js', get_template_directory_uri() . '/js/jquery-3.1.0.min.js', array(), '20151112', true );
    wp_enqueue_script( 'custSampSize.js', get_template_directory_uri() . '/js/custSampSize.js', array(), '20151112', true );
    wp_enqueue_script( 'actb.js', get_template_directory_uri() . '/js/actb.js', array(), '20151112', true );
    // wp_enqueue_script( 'color-scheme-control', get_template_directory_uri() . '/js/color-scheme-control.js', array(), '20151112', true );
    //wp_enqueue_script( 'compSUSRegress', get_template_directory_uri() . '/js/compSUSRegress.js', array(), '20151112', true );
    //wp_enqueue_script( 'customize-preview', get_template_directory_uri() . '/js/customize-preview.js', array(), '20151112', true );
    wp_enqueue_script( 'html5', get_template_directory_uri() . '/js/html5.js', array(), '20151112', true );
    //wp_enqueue_script( 'keyboard-image-navigation', get_template_directory_uri() . '/js/keyboard-image-navigation.js', array(), '20151112', true );
    wp_enqueue_script( 'klm', get_template_directory_uri() . '/js/klm.js', array(), '20151112', true );
    wp_enqueue_script( 'myscript', get_template_directory_uri() . '/js/myscript.js', array(), '20151112', true );
    wp_enqueue_script( 'myselect2', get_template_directory_uri() . '/js/myselect2.js', array(), '20151112', true );
    //wp_enqueue_script( 'npsMeanRegress', get_template_directory_uri() . '/js/npsMeanRegress.js', array(), '20151112', true );

    if(is_single(230))
    {
    wp_enqueue_script( 'npsMeanRegress', get_template_directory_uri() . '/js/npsMeanRegress.js', array(), '20151112', true );
    wp_enqueue_script( 'npsSUSMeanRegress', get_template_directory_uri() . '/js/npsSUSMeanRegress.js', array(), '20151112', true );
    }

    wp_enqueue_script( 'organic', get_template_directory_uri() . '/js/organic.js', array(), '20151112', true );
    wp_enqueue_script( 'skip-link-focus-fix', get_template_directory_uri() . '/js/skip-link-focus-fix.js', array(), '20151112', true );
    wp_enqueue_script( 'jquery-1.10.2', get_template_directory_uri() . '/js/jquery-1.10.2.js', array(), '20151112', true );
    /* 09.02.2017 end */
    wp_enqueue_style( 'custom-style', get_template_directory_uri() . '/css/custom.css', array(), false, '' );


    if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
        wp_enqueue_script( 'comment-reply' );
    }
    if ( is_singular() && wp_attachment_is_image() ) {
        wp_enqueue_script( 'twentysixteen-keyboard-image-navigation', get_template_directory_uri() . '/js/keyboard-image-navigation.js', array( 'jquery' ), '20151104' );
    }
    wp_enqueue_script( 'twentysixteen-script', get_template_directory_uri() . '/js/functions.js', array( 'jquery' ), '20151204', true );
    wp_enqueue_script( 'twentysixteen-jquery', get_template_directory_uri() . '/js/jquery-1.12.0.min.js', array( 'jquery' ), '', true );
    wp_enqueue_script( 'twentysixteen-boot', get_template_directory_uri() . '/js/bootstrap.js', array( 'jquery' ), '', true );
    wp_enqueue_script( 'twentysixteen-owl', get_template_directory_uri() . '/js/owl.carousel.js', array( 'jquery' ), '', true );
    wp_enqueue_script( 'twentysixteen-main', get_template_directory_uri() . '/js/main.js', array( 'jquery' ), '', true );
    wp_localize_script( 'twentysixteen-script', 'screenReaderText', array(
        'expand'   => __( 'expand child menu', 'twentysixteen' ),
        'collapse' => __( 'collapse child menu', 'twentysixteen' ),
    ) );
}
add_action( 'wp_enqueue_scripts', 'twentysixteen_scripts' );

All of this works. Now, I want to enqueue yet another file. Additionally, I wanted to make it load conditionally, anywhere but front page, but at this point I'd be very happy by making it load at all!

First, I tried the conditional version (if ! is_front_page...) but didn't work (I made it work on header.php, but I want it to load after everything else). Thinking there might be an error in my code, I tried enqueuing the file just to see it loads.... and nothing. Tried everything I could think of... and nothing.

So, as a last resource, I tried replacing one of the files that actually loads, custom-style --> css/custom.css and as weird as it sounds, it does nothing. Furthermore, if I check the source, even so css/custom.css shouldn't be there, it still is. Just in case: I don't have any cache and I can see any other changes that are not related to this file.

Finally, I tried to create a different function to enqueue files and nothing, it doesn't work at all.

This is the code I tried to add with no luck:

{wp_enqueue_style( 'blog-styles', get_template_directory_uri() . 'full-style.css', array( 'twentysixteen-style' ), '20170410' );}

Finally, based on this answer I tried this, which would also help with the conditional part:

add_action('init', 'my_register_styles');

function my_register_styles() {
    wp_register_style( 'style1', get_template_directory_uri() . '/style.css' );
    wp_register_style( 'style2', get_template_directory_uri() . '/full-style.css' );
}

add_action( 'wp_enqueue_scripts', 'my_enqueue_styles' );

function my_enqueue_styles() {
    if ( is_front_page() ) {
        wp_enqueue_style( 'style1' );
    } else {
        wp_enqueue_style( 'style2' );
    }
}

but as you probably imagine by now.... it didn't work!

Question(s)

  • What is causing this? What am I doing wrong?
  • Is there a way to make the stylesheet load on header.php AFTER the enqueued scripts?
  • Hi, do you have debugging turned on? And if you inspect your browser do you have any errors? And maybe what is your site? – ngearing Apr 11 '17 at 0:10
  • I had to make an edit to my answer. I originally suggested wp_dequeue_script(), but I meant to use wp_dequeue_style() so be sure to user the correct one if you are testing it. – iyrin Apr 11 '17 at 3:12
0

Edit: I meant to refer to wp_dequeue_style()

You have to dequeque (unregister) the scripts or styles that you wish to replace before replacing them or the new parameters won't be accepted. Use wp_dequeue_style(). Also, as it says in the description for wp_dequeue_script(), hook it with a late priority (100), so that it is after the script was enqueued.

I believe your function my_enqueue_styles() is where you would want to make use of wp_dequeue_style() just before wp_enqueue_style() is used. Something like this:

function my_enqueue_styles() {
    if ( is_front_page() ) {
        wp_dequeue_style( 'style1' );
        wp_enqueue_style( 'style1' );
    } else {
        wp_dequeue_style( 'style1' );
        wp_enqueue_style( 'style2' );
    }
}

Then add the integer 100 as an argument in your action hook. add_action( 'wp_enqueue_scripts', 'my_enqueue_styles', 100 );

Deque any other scripts or styles you wish to enqueue that have already been enqueued.

|improve this answer|||||
  • 1
    thank you, that really made it! I can't vote up, but I'd give you 1000 votes, ha ha! – Rick Alvarez Apr 11 '17 at 16:47
0

Are you sure you are changing the correct functions.php file? You can also check if there is any other header-{xxx}.php in your theme which may be preventing the enqueue scripts from triggering.

One last thing you can try is call the wp_enqueue_style() in the template file (like the page.php or a page-template.php). Doing so will load that style only when that template page is requested.

|improve this answer|||||

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.