Context
I built a child theme based on Twenty Thirteen which works quite well. After updating the parent theme to version 1.3, I noticed strange behavior with the styling which was caused by a cached parent theme's style.css
.
Here is the content of my child theme's style.css
(omitting headers)
/* =Imports styles from the parent theme
-------------------------------------------------------------- */
@import url('../twentythirteen/style.css');
So the child theme's style.css
does nothing more than import the parent theme's style.css
.
I also have another css file with my child theme's customizations which I enqueue like so in functions.php
:
// Enqueue parent theme's style.css (faster than using @import in our style.css)
$themeVersion = wp_get_theme()->get('Version');
// Enqueue child theme customizations
wp_enqueue_style('child_main', get_stylesheet_directory_uri() . '/css/main.css',
null, $themeVersion);
This gives me a very nice css url like this: domain.com/wp-content/themes/toutprettoutbon/css/main.css?ver=1.0.1
that makes sure the style sheet is reloaded when the child theme is updated.
Now the problem
The statement @import url('../twentythirteen/style.css');
is completely independent of the underlying parent theme's version. In fact, the parent theme can be updated without updating the child theme but browsers will still use cached versions of the old ../twentythirteen/style.css
.
Relevant code in Twenty Thirteen that enqueues the style.css
:
function twentythirteen_scripts_styles() {
// ...
// Add Genericons font, used in the main stylesheet.
wp_enqueue_style( 'genericons', get_template_directory_uri() . '/genericons/genericons.css', array(), '3.03' );
// Loads our main stylesheet.
wp_enqueue_style( 'twentythirteen-style', get_stylesheet_uri(), array(), '2013-07-18' );
// Note usage of get_stylesheet_uri() which actually enqueues child-theme/style.css
// Loads the Internet Explorer specific stylesheet.
wp_enqueue_style( 'twentythirteen-ie', get_template_directory_uri() . '/css/ie.css', array( 'twentythirteen-style' ), '2013-07-18' );
}
add_action( 'wp_enqueue_scripts', 'twentythirteen_scripts_styles' );
I can think of a few ways to solve this problem but none are really satisfactory:
Update my child theme every time the parent theme is updated to change a version string in
style.css
(e.g.@import url('../twentythirteen/style.css?ver=NEW_VERSION');
). This creates an unnecessary and annoying link between parent theme version and child.In my child's
functions.php
, 1)wp_dequeue_style
the included child theme'sstyle.css
and 2)wp_enqueue_style
the parent theme'sstyle.css
directly WITH version string. This messes up the order of queued css in the parent theme.Use the
style_loader_tag
filter to modify the generated css<link>
tag forstyle.css
and modify the path to point directly to the parent theme'sstyle.css
WITH a version string. Seems rather obscure for such a common need (cache busting).Dump the parent theme's
style.css
in my child theme'sstyle.css
. Same as (1) really, but a bit faster.Make my child theme's
style.css
be a symlink to the parent theme'sstyle.css
. This seems quite hackish...
Have I missed something? Any suggestions?
edit
Added genericicons.css
and ie.css
style sheets in parent theme to clarify why I can't change the @import
css statement to wp_enqueue_style
in my child theme. Currently, with an @import
statement in my child theme's style.css
, I have this order in generated pages:
- twentythirteen/genericons/genericons.css -> enqueued by parent theme
- child-theme/style.css -> enqueued by parent theme, @imports twentythirteen/style.css
- twentythirteen/css/ie.css -> enqueued by parent theme
- child-theme/css/main.css -> enqueued by child theme
If I enqueue the parent's style.css
as a dependency of main.css
, this will become:
- twentythirteen/genericons/genericons.css -> enqueued by parent theme
- child-theme/style.css -> empty, enqueued by parent theme
- twentythirteen/css/ie.css -> enqueued by parent theme
- twentythirteen/style.css -> enqueued by child theme as dependency of main.css
- child-theme/css/main.css -> enqueued by child theme
Note that ie.css is now included before the parent theme's style.css
. I do not want to change the enqueuing order of the parent theme's css files because I cannot presume that this won't cause problems with the priority of css rules.
@import
, set the parent theme’s stylesheet as a dependency of your own stylesheet instead.style.css
would not be included at the same place as it is now. The parent includes other css files which must come between itsstyle.css
and my child theme's css.