I wanted to start automatically changing my enqueue version so I don't have to manually change it after a file edit so I thought about using filemtime()
to pull the time but for some reason when I use it with site_url()
or home_url
it doesn't work:
function bootstrap_enqueue() {
$bootstrap_file = home_url() . '/css/bootstrap.css';
if (file_exists($bootstrap_file)) :
$bootstrap_ver = date("y.m.d", filemtime($bootstrap_file));
else :
$bootstrap_ver = '1.0.0';
endif;
wp_enqueue_style('bootstrap-style', $bootstrap_file, $bootstrap_ver);
}
add_action('wp_enqueue_scripts', 'bootstrap_enqueue');
but when I pass:
wp_enqueue_style('bootstrap-style', home_url() . '/css/bootstrap.css', '1.0' );
it works. I've researched and read:
- Alternative to WordPress's get_site_url()
- Get the last modified date of a remote file
- Get last modified file in a directory
- filemtime
but I haven't found an answer to why filemtime()
it's work in WordPress with home_url
?
EDIT:
Further testing I've tried:
wp_enqueue_style('bootstrap-style', $bootstrap_file, array(), $bootstrap_ver);
thinking it might be a sequencing issue but still doesn't work. I've moved the CSS file into the theme's directory and tried:
$bootstrap_file = get_template_directory_uri() . '/css/bootstrap.css';
if (file_exists($bootstrap_file)) :
$bootstrap_ver = date("y.m.d", filemtime($bootstrap_file));
else :
$bootstrap_ver = '1.0.0';
endif;
wp_enqueue_style('bootstrap-style', $bootstrap_file, array(), $bootstrap_ver);
all of them are still producing the same result and the version is being pushed to 1.0.0
so I think it has something to do with $bootstrap_file = home_url() . '/css/boostrap.css';
just not sure what.
In the head I'm returned what appears to be correct:
<link rel='stylesheet' id='bootstrap-style-css' href='http://path/to/server/css/bootstrap.css?ver=1.0.0' type='text/css' media='all' />
but the Bootstrap file isn't rendering.