Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

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!

share|improve this question
1  
This ticket (and associated commits) may be of interest to you: core.trac.wordpress.org/ticket/20103 –  helenhousandi Mar 27 '12 at 17:18
add comment

1 Answer

up vote 1 down vote accepted

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.

share|improve this answer
    
Great answer, thanks! I'll actually just cache it in-process using a PHP static variable (I mostly just not want multiple file parsings for each page view). –  julien_c Mar 27 '12 at 17:37
    
I usually advise against using static vars for caching ... it's easy to forget to remove them and, if the PHP process is long-running, you can easily create a memory leak by mistake when old static vars hang around. –  EAMann Mar 27 '12 at 17:40
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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