22

I was trying to move the load of my style.css in the footer of my wordpress theme. I have already done something similar with 2 js files and it works fine, they load in the footer:

function my_init() {
    if (!is_admin()) {
        wp_deregister_script('jquery');
        wp_register_script('jquery', 'http://www.studionews24.com/wp-content/themes/network/js/jquery.min.js', false, '1.3.2', true);
        wp_enqueue_script('jquery');

        // load a JS file from my theme: js/theme.js
        wp_enqueue_script('my_script', 'http://www.studionews24.com/wp-content/themes/network/js/menu-resp.js', array('jquery'), '1.0', true);
    }
}
add_action('init', 'my_init');

Now i was trying to move in the footer also the style.css, and keep only some inline css rules in the tag. I have tried wp_enqueue_style but it seems doesn't work well for me.

Someone could help me for find a smart solution?

0

1 Answer 1

41

Actually all styles should be placed in header. So WordPress doesn't have a parameter for doing this in the wp_enqueue_style function, because traditionally all styles were added in the head. Recently, many sites have moved to a system where critical "above the fold" styles are loaded in the head, while other styles are loaded in the footer.

So here is a way to to this: You can use print_late_styles() function which is called in footer. You just need to enqueue your styles when header is already passed.

So you need to find some hook which is called on each page and after wp_head hook. For example get_footer could be one.

function prefix_add_footer_styles() {
    wp_enqueue_style( 'your-style-id', get_template_directory_uri() . '/stylesheets/somestyle.css' );
};
add_action( 'get_footer', 'prefix_add_footer_styles' );
4
  • But if i want to force the style in the footer how could i do? There is really no way to do that? May 1, 2015 at 16:33
  • print_late_styles() could be used for that, not that you should though, it »Prints the styles that were queued too late for the HTML head.«. @GiulioBambini May 1, 2015 at 16:39
  • @Emetrop could you please be so kind to show me an example of code about print_late_style functionality? May 1, 2015 at 16:50
  • 36
    Actually its a myth that styles need to be in the head, styles that are essential for the initial page render should be in the head. Other not so critical styles can be loaded at the bottom to optimize for page-load speed. Feb 25, 2017 at 20:53

Not the answer you're looking for? Browse other questions tagged or ask your own question.