Last active
January 21, 2025 22:59
-
-
Save nicksleap/2ada8afd978d969d9d420aac279db09c to your computer and use it in GitHub Desktop.
WordPress Helper Functions: Enqueue Styles and Scripts with Automatic Cache Busting
This file contains hidden or 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
/** | |
* Includes CSS styles with automatic file version specification. | |
* | |
* @param string $name Unique style name for registration. | |
* @param string $path Path to style file relative to theme directory. | |
* @param array $dep List of dependent styles (empty by default). | |
* @return void|false Returns wp_enqueue_style() result or false if file not found. | |
*/ | |
function theme_prefix_enqueue_style($name, $path, $dep = []) | |
{ | |
$dir_uri = get_template_directory_uri(); | |
$dir = get_template_directory(); | |
$path = '/' . ltrim($path, '/'); | |
$mtime = file_exists($dir . $path) ? filemtime($dir . $path) : false; | |
return wp_enqueue_style( | |
$name, | |
$dir_uri . $path, | |
$dep, | |
$mtime | |
); | |
} | |
/** | |
* Includes JavaScript files with automatic file version specification. | |
* | |
* @param string $name Unique style name for registration. | |
* @param string $path Path to style file relative to theme directory. | |
* @param array $dep List of dependent styles (empty by default). | |
* @return void|false Returns wp_enqueue_script() result or false if file not found. | |
*/ | |
function theme_prefix_enqueue_script($name, $path, $dep = []) | |
{ | |
$dir_uri = get_template_directory_uri(); | |
$dir = get_template_directory(); | |
$path = '/' . ltrim($path, '/'); | |
$mtime = file_exists($dir . $path) ? filemtime($dir . $path) : false; | |
return wp_enqueue_script( | |
$name, | |
$dir_uri . $path, | |
$dep, | |
$mtime | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment