7

I am using filemtime() to add a version number for different .css and .js files in a custom WordPress theme.

I used the accepted answer described here as a guideline; however, I use the wp_retister_style() and wp_register_script() functions to register my .css and .js files respectively, and evetually load them via wp_enqueue_style() and wp_enqueue_script(). As an example, I am registering sigle.css file this way:

wp_register_style('xyz_single', get_template_directory_uri() . '/css/single.css', false, filemtime(get_template_directory() . '/css/single.css'));

The fourth parameter in the function above will add ?ver=nnnnnnnn to the stylesheet. The final output will be something like single.css?ver=nnnnnnnn, where nnnnnnnn is a number representing the timestamp of the modified file in order to esure reloading it when I modify the theme so caching issues are taken care off.

Now, to my question.

In my custome theme, I use rtl.css, which is loaded automatically for me when wp_head() is called. So how do I append rtl.css to include a version number?


More information

In header.php, I am loading style.css as follows:

<link href="<?php echo get_stylesheet_uri() . '?ver=' . filemtime(get_stylesheet_directory() . '/style.css'); ?>" rel="stylesheet" type="text/css" />

I am loading all other stylesheets using wp_register_style() as described above, followed by wp_enqueue_style(). Here is an example:

wp_register_style('xyz_single', get_template_directory_uri() . '/css/single.css', false, filemtime(get_template_directory() . '/css/single.css'));

wp_enqueue_style('xyz-single');

However, rtl.css is automatically loaded when I call wp_head() in header.php. Please read this great article on how wp_head() loads rtl.css; and hence my question, how can I add a version number to rtl.css if it is called automatically?

  • 2
    how is it being loaded? please edit the question and explain. Please also explain the context, that prevents you from handling rtl.css the same way you handle style.css – Mark Kaplun Feb 7 '15 at 9:54
  • @MarkKaplun - Thanks, I updated the question. I hope this explains it. – Greeso Feb 7 '15 at 10:04
6

You are loading the rtl.css using the WordPress automatic way, that is, having a rtl.css in the theme folder will be loaded by WordPress automatically if it presents and direction of current language ir rtl (you should add this information to the question, it has been difficult to figure it out how you are loading the file). This process defines locale_stylesheet_uri that you can use to modify the locale stylesheet URL, so you can add the version parameter to it:

add_filter( 'locale_stylesheet_uri', function ($localized_stylesheet_uri) {
    $time_ver = filemtime( get_stylesheet_directory() . '/rtl.css' );
    return add_query_arg( array('ver' => $time_ver), $localized_stylesheet_uri );
});

If you need also handle dependencies you will have to load rlt.css file with wp_enqueue_style just like any other css file. No way of handling dependencies with the "auto" way.

2

Based on the article you have pointed to, it seems like there are two simple options

  1. prevent the automatic inclusion by removing the actions that makes it to be added,

remove_action( 'wp_head', 'locale_stylesheet' );

and enqueue it with your own code, something like

global $$wp_locale; 

$stylesheet_dir_uri = get_stylesheet_directory_uri();
$dir = get_stylesheet_directory();
$locale = get_locale();


if ( !empty($wp_locale->text_direction) && file_exists("$dir/{$wp_locale->text_direction}.css") )
        enqueue "$stylesheet_dir_uri/{$wp_locale->text_direction}.css";
  1. use the 'locale_stylesheet_uri' filter, and add whatever suffix you prefer to the url, or just return '' and enqueue it by yourself like in #1

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.