Is the current Theme version number cached somewhere?

I need access to my Theme’s version number a number of times on each Admin Page view.

Right now I’m using get_theme_data which in turn parses the Theme’s style.css file via get_file_data, so style.css gets parsed multiple times.

Read More

Is there a built-in caching mechanism for this data? Or should I just build a thin in-process caching wrapper around get_theme_data?

Thanks!

Related posts

Leave a Reply

1 comment

  1. In the mean time, keep using your method. But as @helenhousandi linked in a comment, WordPress 3.4 will be introducing a new WP_Theme object that will make this much easier.

    Right now, you’re using get_theme_data() to return an array with the theme’s version. No, it’s not cached … but you could likely cache it yourself in a transient if you’re accessing it a lot.

    function my_get_theme_version() {
        $value = get_transient( 'theme_version' );
        if ( !$value ) {
            $info = get_theme_data( /* theme filename */ );
            $value = $info[ 'Version' ];
    
            // Cache the value for 12 hours.
            set_transient( 'theme_version', $value, 60 * 60 * 12 );
        }
    
        return $value;
    }
    

    But in WP 3.4, get_theme_data() will be deprecated and will instead serve as a wrapper for the new WP_Theme API (meaning the above code will still work, but will trigger a deprecation notice).

    So instead, you could use this:

    function my_get_theme_version() {
        $theme_file = /* theme filename */;
        $theme = new WP_Theme( basename( dirname( $theme_file ) ), dirname( dirname( $theme_file ) ) );
    
        return $theme->get('Version');
    }
    

    The new object has some level of caching built in. I encourage you to read the discussion surrounding the new changes and to follow the Codex for when it’s officially documented as well.