0

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?

2
  • You're not calling your name_enqueuer() function in name_enqueue_styles(), only in name_enqueue_scripts(). Commented Sep 29, 2022 at 20:49
  • Because you haven't passed the version in the wp_enqueue_style function Commented Sep 29, 2022 at 20:53

2 Answers 2

1

add it to the function

wp_enqueue_style( 'style', get_template_directory_uri() . '/dist/css/main.css', array(), '1.0');
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.