0

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:

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.

DᴀʀᴛʜVᴀᴅᴇʀ
  • 7,681
  • 17
  • 73
  • 127
  • When you use `http://path/to/server/css/boostrap.css?ver=1.0.0` as a URL in your browser does it show the css file? Have you tried using only `'/css/boostrap.css'`, because `/` will take to the root of the website. – WizardCoder Sep 22 '17 at 21:57
  • yes the file renders but for some reason it isn't pulling the version date. – DᴀʀᴛʜVᴀᴅᴇʀ Sep 22 '17 at 21:58
  • Try this `bootstrap_file = $_SERVER["DOCUMENT_ROOT"] . '/css/boostrap.css';` (The CSS would need to go back in the root for this example) If that doesn't work try turning on PHP error reporting. It may show you some useful errors. – WizardCoder Sep 22 '17 at 22:04

3 Answers3

1

I think the reason is that PHP filesystem functions don't go off of URLs but absolute paths to files. So we need both the URL and the Absolute Path to the file so we can ensure it exists and get the file timestamp:

function bootstrap_enqueue() {

    // Roots
    // $bootstrap_abs   = ASBSPATH  . '/css/bootstrap.css';
    // $bootstrap_url   = home_url() . '/css/bootstrap.css';

    $bootstrap_abs  = get_stylesheet_directory() . '/css/bootstrap.css';
    $bootstrap_url  = get_stylesheet_directory_uri() . '/css/bootstrap.css';

    if( file_exists( $bootstrap_abs ) ) :
        $bootstrap_ver = date( "y.m.d", filemtime( $bootstrap_abs ) );
    else :
        $bootstrap_ver = '1.0.0';
    endif;

    wp_enqueue_style('bootstrap-style', $bootstrap_url, array(), $bootstrap_ver);
}
add_action('wp_enqueue_scripts', 'bootstrap_enqueue');
Howdy_McGee
  • 10,422
  • 29
  • 111
  • 186
0

The 'home_url' function echos out the home URL, which will break your code. Use get_home_url() instead, because that function returns the value, which means you can store it in a variable or echo it.

wp_enqueue_style('bootstrap-style', get_home_url() . '/css/bootstrap.css', '1.0' );

EDITED

The PHP filemtime() function needs to get the files URL from the server root, but the wordpress wp_enqueue_style() function needs the host root URL. The following changes to your code might work as long as if your CSS is still in your root directory. If it is is your theme directory just change get_home_url() to get_template_directory_uri(). If that doesn't work, then smoke me a kipper, I'll be back for breakfast.

function bootstrap_enqueue() {
    $bootstrap_file = get_home_url() . '/css/bootstrap.css';
    if (file_exists($bootstrap_file)) :
        $bootstrap_ver = date("y.m.d", filemtime($_SERVER["DOCUMENT_ROOT"] . '/css/bootstrap.css'));
    else :
        $bootstrap_ver = '1.0.0';
    endif;
    wp_enqueue_style('bootstrap-style', $bootstrap_file, $bootstrap_ver);
}
add_action('wp_enqueue_scripts', 'bootstrap_enqueue');
WizardCoder
  • 3,353
  • 9
  • 20
0

Close -- I believe the issue is that filemtime needs a file path asset, whereas you're trying to feed it a web URL.

I'd try something like using getcwd() to point to the file instead.

DᴀʀᴛʜVᴀᴅᴇʀ
  • 7,681
  • 17
  • 73
  • 127
Greg Burkett
  • 1,905
  • 1
  • 12
  • 14