Home > Net >  adding versions to css files doesn't work
adding versions to css files doesn't work

Time:10-01

I have this code in my functions.php file that should add the version number to both css and js files like for example ..../scripts.js?ver=1664475399. But it only works with js files. The css file is showing the wordpress version at the end of its name ex. ?ver=6.0.2

function name_enqueuer($my_handle, $relpath, $type='script', $my_deps=array(), $in_footer) {
    $uri = get_theme_file_uri($relpath);
    $vsn = filemtime(get_theme_file_path($relpath));
    if($type == 'script') wp_enqueue_script($my_handle, $uri, $my_deps, $vsn, $in_footer);
    else if($type == 'style') wp_enqueue_style($my_handle, $uri, $my_deps, $vsn);
}


/* Add styles */
function name_enqueue_styles() {
    wp_enqueue_style( 'style', get_template_directory_uri() . '/dist/css/main.css');
  }
add_action( 'wp_enqueue_scripts', 'name_enqueue_styles');


/* Add scripts */
function name_enqueue_scripts() {
    name_enqueuer('my-scripts', '/dist/js/scripts.js', 'script', '1.0', true);
  }
add_action( 'wp_enqueue_scripts', 'name_enqueue_scripts');

what could cause this problem?

CodePudding user response:

add it to the function

wp_enqueue_style( 'style', get_template_directory_uri() . '/dist/css/main.css', array(), '1.0');

CodePudding user response:

function get_mod_time($file) {
// Convert web link to absolute path
if ( substr($file, 0, 7) == 'http://' || substr($file, 0, 8) == 'https://' ) {
    $file = str_replace( home_url('/'), ABSPATH, $file );
}

if (file_exists($file)) {
    return date("ymd-Hi", filemtime($file));
}
else {
    return date('y.m.d-H');
}

}

wp_enqueue_style( 'site-css', get_template_directory_uri() . '/assets/css/style.min.css', array(), get_mod_time(get_template_directory_uri() . '/assets/css/style.css'), 'all' );

  • Related