Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 400
nonhocapito Avatar asked Oct 29 '25 15:10

nonhocapito


1 Answers

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 );
like image 60
christian Nguyen Avatar answered Oct 31 '25 07:10

christian Nguyen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!