Last active
October 9, 2024 00:37
-
-
Save kellenmace/5ce9909d37d37521481b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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' ) | |
); |
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.
hi, sorry cant figure out, where to put it?
@sanderlt You can put calls to wp_enqueue_style()
and wp_enqueue_script()
inside of a function that is hooked to WordPress' wp_enqueue_scripts
hook. These examples show how to do that: https://developer.wordpress.org/reference/functions/wp_enqueue_style/#user-contributed-notes
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
perfect, thanks!