简体   繁体   中英

Why does home_url() not work with filemtime() in WordPress?

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'; $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.

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');

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');

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM