WordPress.org

Ready to get started?Download WordPress

Forums

add version number to style.css in custom theme (7 posts)

  1. Matthew Taylor
    Member
    Posted 1 year ago #

    I'm only doing this during development. It's to keep any browser from caching the custom theme stylesheet.

    function css_versioning() {
     wp_enqueue_script( 'style-css', get_stylesheet_directory_uri() . '/style.css' , false,filemtime( get_stylesheet_directory() . '/style.css' ), false );
     }
    add_action( 'wp_print_styles', 'css_versioning' );

    What I get in the header is:

    <link rel="stylesheet" id="style-css" href="http://mrtwebdesign.local/ddd/wp-content/themes/dlab/style.css?ver=3.5.1" type="text/css" media="all">
    <script type="text/javascript" src="http://mrtwebdesign.local/ddd/wp-content/themes/dlab/style.css?ver=1366413348"></script>

    I would like to just get the second result. I want to make sure any changes are loading for my client.

  2. Matthew Taylor
    Member
    Posted 1 year ago #

    update: let's try the correct WP function:

    function css_versioning() {
     wp_enqueue_style( 'style-css', get_stylesheet_directory_uri() . '/style.css' , false,filemtime( get_stylesheet_directory() . '/style.css' ), 'all' );
     }
    add_action( 'wp_print_styles', 'css_versioning' );

    better.. but still 2 entries:

    <link rel="stylesheet" id="style-css" href="http://mrtwebdesign.local/ddd/wp-content/themes/dlab/style.css?ver=3.5.1" type="text/css" media="all">
    <link rel="stylesheet" id="style-css-css" href="http://mrtwebdesign.local/ddd/wp-content/themes/dlab/style.css?ver=1366413348" type="text/css" media="all">

    How do I get down to just the one 'style.css' with my version number(not the WordPress version number)?

  3. Maruti Mohanty
    Member
    Posted 1 year ago #

    You are seeing two stylesheet because one is added by default by wordpress selected theme and you are adding the other.

    If you have any custom stylesheet then try something like this

    function css_versioning() {
    wp_enqueue_style( 'custom', get_stylesheet_directory_uri() . '/style.css' , false,filemtime( get_stylesheet_directory() . '/custom.css' ), 'all' );
    }
    add_action( 'wp_enqueue_scripts', 'css_versioning' );

    But before that what is your need actually? Why would you need to change the version of the default style.css file

  4. Matthew Taylor
    Member
    Posted 1 year ago #

    The default css file is the style.css of the theme. It's a custom theme. I need approvals from my client. We often have trouble getting the style.css to update due to browser caching. So if I make major changes it's possible we won't see the same thing. That's why the version number is the last time the style.css file was changed. It won't break caching for that file unless it's been saved by me. I like this solution, because it's not something I have to remember to remove before going live. It's not really the kind of thing the will have a performance hit.

    I think I found my problem. I wasn't careful about where I dropped the function into my functions.php file.

    It was being put in to the functions file after:
    if ( ! function_exists( 'dlab_setup' ) ) :
    That means it was being called after the 'after_setup_theme' hook. The default inclusion of style.css has already been added to wp_head() and the wp_enqueue_style ends up tacking on another link to the same stylesheet.

    A silly mistake. But I'm glad I was able to figure it out.

  5. Maruti Mohanty
    Member
    Posted 1 year ago #

    Glad that you have figured it out .

    Would you mind sharing the snippet, how do you change/control the version of the default style.css ??

    Thanks :)

  6. strangerstudios
    Member
    Posted 12 months ago #

    What I typically do is take all of the CSS out of my style.css file and then use functions.php to wp_enqueue_style() my stylesheet with a version added.

    However, I just discovered another way to update the version added to the style.css file loaded from the template.

    You use the wp_default_styles filter here:
    https://github.com/WordPress/WordPress/blob/0fa23c1ac2f46ab79c186f4a295648f990758636/wp-includes/class.wp-styles.php#L32

    Notice that this is called with do_action_ref_array and the styles are passed by reference. So you don't need your callback to return a value. Just updated the object directly. Like so:

    function my_wp_default_styles($styles)
    {
    	//use release date for version
    	$styles->default_version = "20130711";
    }
    add_action("wp_default_styles", "my_wp_default_styles");

    By default the version used is the overall WP version. This code above will update the ?ver= added to the end of any stylesheet enqueued without a version, including the style.css loaded from the active theme.

    Cheers.

  7. Brian Hogg
    Member
    Posted 5 months ago #

    Thanks Jason, this worked great when making theme updates without a WP version update!

Topic Closed

This topic has been closed to new replies.

About this Topic