Skip to content

Instantly share code, notes, and snippets.

@macbookandrew
Last active June 15, 2022 19:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save macbookandrew/cb547495fbe04e4924fa7e1c5db5a635 to your computer and use it in GitHub Desktop.
Save macbookandrew/cb547495fbe04e4924fa7e1c5db5a635 to your computer and use it in GitHub Desktop.
WordPress theme stylesheet auto cache-busting
<?php
define( 'MY_THEME_VERSION', '1.4' );
/**
* Enqueue stylesheet
*
* Uses a constant that has to be manually updated
*/
function my_theme_enqueue_assets() {
wp_enqueue_style( 'stylesheet', get_stylesheet_uri(), array(), MY_THEME_VERSION );
// stylesheet URL: https://mydomain.com/wp-content/themes/my-theme/style.css?ver=1.4
}
add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_assets' );
<?php
/**
* Enqueue stylesheet
*
* Uses the modification timestamp of the style.css file
*/
function my_theme_enqueue_assets() {
wp_enqueue_style( 'stylesheet', str_replace( '.css', '.' . filemtime( get_stylesheet_directory() . '/style.css' ) . '.css', get_stylesheet_uri() ) );
// stylesheet URL: https://mydomain.com/wp-content/themes/my-theme/style.1518212243.css
}
add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_assets' );
<?php
/**
* Enqueue stylesheet
*
* Uses the modification timestamp of the style.css file
*/
function my_theme_enqueue_assets() {
wp_enqueue_style( 'stylesheet', get_stylesheet_uri(), array(), filemtime( get_stylesheet_directory() . '/style.css' ) );
// stylesheet URL: https://mydomain.com/wp-content/themes/my-theme/style.css?ver=1518212243
}
add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_assets' );
/**
* Theme Name: Twenty Seventeen
* Theme URI: https://wordpress.org/themes/twentyseventeen/
* Author: the WordPress team
* Author URI: https://wordpress.org/
* Description: Twenty Seventeen brings your site to life with header video and immersive featured images. With a focus on business sites, it features multiple sections on the front page as well as widgets, navigation and social menus, a logo, and more. Personalize its asymmetrical grid with a custom color scheme and showcase your multimedia content with post formats. Our default theme for 2017 works great in many languages, for any abilities, and on any device.
* Version: 1.4
* License: GNU General Public License v2 or later
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: twentyseventeen
* Tags: one-column, two-columns, right-sidebar, flexible-header, accessibility-ready, custom-colors, custom-header, custom-menu, custom-logo, editor-style, featured-images, footer-widgets, post-formats, rtl-language-support, sticky-post, theme-options, threaded-comments, translation-ready
* This theme, like WordPress, is licensed under the GPL.
* Use it to make something cool, have fun, and share what you've learned with others.
*/
// or at the very least:
/**
* Theme Name: Twenty Seventeen
* Version: 1.4
*/
<?php
/**
* Autocalculate and define theme version constant for use in multiple stylesheets/scripts
*/
define( 'MY_THEME_VERSION', wp_get_theme()->get( 'Version' ) );
/**
* Enqueue stylesheet
*
* Uses the Version header from style.css
*/
function my_theme_enqueue_assets() {
wp_enqueue_style( 'stylesheet', str_replace( '.css', '.' . MY_THEME_VERSION . '.css', get_stylesheet_uri() ) );
// stylesheet URL: https://mydomain.com/wp-content/themes/my-theme/style.1.4.css
}
add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_assets' );
<?php
/**
* Auto-calculate and define theme version constant for use in multiple stylesheets/scripts
*/
define( 'MY_THEME_VERSION', wp_get_theme()->get( 'Version' ) );
/**
* Enqueue stylesheet
*
* Uses the Version header from style.css
*/
function my_theme_enqueue_assets() {
wp_enqueue_style( 'stylesheet', get_stylesheet_uri(), array(), MY_THEME_VERSION );
// stylesheet URL: https://mydomain.com/wp-content/themes/my-theme/style.css?ver=1.4
}
add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_assets' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment