Skip to content

Instantly share code, notes, and snippets.

@knolaust
Last active January 30, 2024 14:53
Show Gist options
  • Save knolaust/f19ab0245f9dba3d402b48a4a89714a4 to your computer and use it in GitHub Desktop.
Save knolaust/f19ab0245f9dba3d402b48a4a89714a4 to your computer and use it in GitHub Desktop.
Enqueue example for WordPress with filemtime
<?php
/**
* Enqueue Styles and Scripts for My Theme
*
* This function enqueues styles and scripts for the custom theme.
* It includes versioning based on the file's last modification time
* to prevent caching and ensures proper dependencies.
*
* Gist Keywords: wordpress, theme, javascript, styles, enqueue
*
* @return void
* @author Knol Aust
*/
function my_theme_enqueue_styles()
{
// Enqueue site styles
wp_enqueue_style('site-css', get_stylesheet_directory_uri() . '/assets/css/style.css', array(), filemtime(get_stylesheet_directory() . '/assets/css/style.css'));
// Enqueue site scripts
wp_enqueue_script('site-js', get_stylesheet_directory_uri() . '/assets/js/scripts.js', array('jquery'), filemtime(get_stylesheet_directory() . '/assets/js/scripts.js'), false);
}
// Hook the function into the 'wp_enqueue_scripts' action
add_action('wp_enqueue_scripts', 'my_theme_enqueue_styles');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment