Search code examples
phpwordpressversioningfilemtime

Trying to add versioning using filemtime but fail


On a wordpress environment.

I'm trying to automatically add versioning to my scripts using filemtime, but get the following error message:

Warning: filemtime() [function.filemtime]: stat failed for (file name)

the code is simple

$myfile = get_template_directory_uri() . '/js/script.js';
wp_enqueue_script('mywebsite-script',$myfile , array( 'jquery' ), filemtime( $myfile ), true );

the path to the file is correct, but as I said I get the stat failed message.

If I add the if (file_exists($myfile)) check, the whole operation is skipped. Yet if I echo the path to $myfile, this is correctly printed and can be opened in the browser!

The path does not contain fancy characters. The server is not on Windows, I've read encoding might be a reason for this but don't know what the workaround should be, if that's the case.

Where's could the problem be?


Solution

  • You should use the real path instead:

    $myfile = get_template_directory_uri() . '/js/script.js';
    $realpath = get_template_directory().'/js/script.js';
    wp_enqueue_script('mywebsite-script',$myfile , array( 'jquery' ), filemtime( $realpath ), true );