2

When I enqueue Bootstrap with wp_enqueue_style(), Bootstrap is loaded after the child theme's style.css and will override style.css. One way to load style.css after Bootstrap is to enqueue style.css as well, but this will cause it to be loaded twice. Any idea how to enqueue style.css once AND after Bootstrap? Thanks!

  • 1
    Add bootstrap.min.css as a dependency to style.css. Enqueuing bootstrap before style.css is easy right, am I missing something here? – bravokeyl Nov 9 '16 at 16:42
  • The childtheme somehow enqueues <code>style.css</code> automatically and I don't know exactly where. If I knew where, I already would have added bootstrap as a dependancy. – newjonnydepony Nov 9 '16 at 16:46
  • Search for wp_enueue_style in your theme folder. I guess the parent theme might be enqueuing it. – bravokeyl Nov 9 '16 at 16:52
5

Mostly the parent theme might be enqueing the child theme's style.css, if so you can dequeue it by using handle and then enqueue with proper dependency.

If the child theme's handle is child-theme-style, then dequeue it using

wp_dequeue_style('child-theme-style')

then enqueue it as needed like so.

wp_enqueue_style('child-theme-dep',get_stylesheet_uri(),array('bootstrap-handle-here'))

enter image description here

We can easily know the child theme stylesheet url by looking at the link output in the head, for example in the attached image.

<link rel='stylesheet' id='hybrid-style-css'  href='https://wptavern.com/wp-content/themes/stargazer-child-dev/style.css?ver=4.6.1' type='text/css' media='all' />

Tells us that the child theme's enqueue handle is hybrid-style which is the id without -css.

3

Good news. This is an easy fix. WordPress allows you to declare dependencies when registering scripts and stylesheets. Simply list the assets you wish to load earlier as dependencies for the later assets.

https://codex.wordpress.org/Function_Reference/wp_register_style

This works for both scripts and styles and is the recommended way to control load order.

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.