I’m making a child theme for TwentyTwelve, but my host is caching my files, so I’m not seeing the changes I do to my style.css file.
Is there a way to version the stylesheet for my child theme?
How is TwentyTwelve even adding the stylesheet? I don’t see it in header.php.
Thanks
How it’s NOT done in WordPress
The following example is the opposite of how one should do it. Bad practice following:
This is another example how you should not do it:
Do it right
WP uses a “dependencies” API for managing script and style files. This means that you
This means that you first go and register a script for further usage and then enqueue it. This way you can register a stylesheet or script in your parent theme or plugin and later on enqueue it and print it to your webpage on demand.
How to prevent caching
This API as well has one nice little argument for versions. This means, that you have a variety of options how to update your browser or server cache, as the version gets added to the query string when loading the file:
date()
ortime()
(or similar DateTime) functions to permanently updatefilemtime( get_stylesheet_directory().'style.css' )
to **only change the version when you did an update..htaccess
modifications as well.Example
This one’s taken from a plugin I’m currently developing:
When to load the stylesheet/script
Normally one simply wraps the register/enqueue/localize statements into a function or class method. To allow others jump in at the right point, the hooks used for styles and scripts are the following:
admin_enqueue_script
// Admin UIwp_enqueue_script
// Themeslogin_enqueue_script
// Login pagesOk, so what I did to fix it, is add a functions.php to my child-theme, and added the following:
So it loads the stylesheet twice, which is not ideal, but while the site is being developed, it helps keep the stylesheet fresh.