13

I am trying to change the stylesheet file version using the filemtime() function with the wp_enqueue_style with the following snippet

function pro_styles()
{
wp_enqueue_style( 'child-style', get_stylesheet_directory_uri() .'/child-style.css', array(), filemtime(get_stylesheet_directory_uri() .'/child-style.css'), 'all' );
}

add_action( 'wp_enqueue_scripts', 'pro_styles' );

but it is throwing a warning

Warning: filemtime(): stat failed for.....

While i am sure that the file exists

2 Answers 2

29

It's because you're retrieving it via URL, but filemtime() requires a path. Use get_stylesheet_directory() instead. That returns a path:

function pro_styles()
{
wp_enqueue_style( 'child-style', get_stylesheet_directory_uri() .'/child-style.css', array(), filemtime(get_stylesheet_directory() .'/child-style.css'), 'all' );
}

add_action( 'wp_enqueue_scripts', 'pro_styles' );
0
5

Just to expand on Jacob Peattie Answer for people that have CSS file in a custom plugin, you can use

filemtime( plugin_dir_path(dirname(__FILE__)).'plugin-folder/css-file-path.css' )
2
  • 1
    great, it works!
    – Iggy
    Commented Jun 25, 2020 at 15:08
  • Good find! Further, if the file is in your own same plugin folder, filemtime(plugin_dir_path(__FILE__) . 'css-file-path.css') suffices.
    – Sygmoral
    Commented May 17, 2023 at 12:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.