Kontakt

Portfolio: Ostryweb @ Instagram.

úterý 12. února 2019

Force refreshing of style changes in Wordpress template (style version as css file attribute)

What we want to achieve is to "force" refreshing of style.css of our template after changes.

Usually this is made by adding template version number as a parameter of the stylesheet, so in the HEAD section the output looks something like this:

<link rel="stylesheet" id="website-style-css" href="https://www.website.cz/wp-content/themes/website/style.css?ver=1.0.1" type="text/css" media="all">
view raw gistfile1.txt hosted with ❤ by GitHub


If we change 1.0.1 to 1.0.2 browsers should download the new version. Now we want to make it simple, and there is a version parameter already present in any Wordpress theme's style.css in its header part, so why not use that? Logically if we make changes in the stylesheet, we should also increment its version number.

To get the stylesheet HTML output as mentioned above, modify enqueuing of the stylesheet in functions.php like this:

wp_enqueue_style( 'website-style', get_stylesheet_uri(), array(), wp_get_theme()->version );
view raw functions.php hosted with ❤ by GitHub


Be aware that you shouldn't have style.css inserted into Wordpress output in any other way. Sometimes it can be "hardcoded" in head.php or other template files.. Check the output HTML if it contains only one line of style.css link tag and that it is the one with the version number...

Žádné komentáře:

Okomentovat