Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
// Version CSS file in a theme
wp_enqueue_style(
'theme-styles',
get_stylesheet_directory_uri() . '/style.css',
array(),
filemtime( get_stylesheet_directory() . '/style.css' )
);
// Version JS file in a theme
wp_enqueue_script(
'theme-scripts',
get_stylesheet_directory_uri() . '/js/scripts.js',
array(),
filemtime( get_stylesheet_directory() . '/js/scripts.js' )
);
// Version CSS file in a plugin
wp_enqueue_style(
'plugin-styles',
plugin_dir_url( __FILE__ ) . 'assets/css/plugin-styles.css',
array(),
filemtime( plugin_dir_path( __FILE__ ) . 'assets/css/plugin-styles.css' )
);
// Version JS file in a plugin
wp_enqueue_script(
'plugin-scripts',
plugin_dir_url( __FILE__ ) . 'assets/js/plugin-scripts.js',
array(),
filemtime( plugin_dir_path( __FILE__ ) . 'assets/js/plugin-scripts.js' )
);
@mitchellkrogza

This comment has been minimized.

Copy link

commented Aug 14, 2016

Excellent, works like a charm. This was the one little thing annoying me about the super awesome caching I have got running on Nginx, now it's just super-awesome-flawless 👍

@donnamcmaster

This comment has been minimized.

Copy link

commented Oct 11, 2018

perfect, thanks!

@apermo

This comment has been minimized.

Copy link

commented Apr 17, 2019

Hey, just stumpled upon your code when I wanted to show this technique to a coworker.

Please mind that get_stylesheet_directory_uri() refers to the childtheme in case you use a childtheme, and is only equivalent if you only have the parent theme.
If you want to make sure it points the parent theme, you need to use get_template_directory_uri() instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.