Enqueue example for WordPress with filemtime

01/30/2024
Back to Blogs
<?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');