SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    In memoriam gold trophysilver trophybronze trophy Dan Schulz's Avatar
    Join Date
    May 2006
    Location
    Aurora, Illinois
    Posts
    15,476
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Is Detection of the Installed Version of WordPress Possible Within a WordPress Theme?

    Hey everyone,

    I'm working on a personal project involving WordPress at the moment. Given that WordPress 2.5 came out not too long ago with some new features that don't work in older installed copies of WordPress (such as built-in Gravatar support, for instance), I've decided that it might be a good idea to detect within a custom WordPress Theme the current installed version of WordPress, and then either enable or deny access to the new feature based on the installed script's version compatibility.

    For example (and I'm not writing this in PHP, just plain conversational language), if I wanted to build into a WordPress Theme the ability for the Theme to use built-in Gravatars, would it be possible for the Theme to say "If the version of WordPress that is using this Theme is equal to or greater than 2.5, enable this feature; otherwise don't even bother trying to waste the server's resources."?

    I'm not a PHP programmer by any stretch of the imagination, and I'm not 100% familiar with the WordPress API (though I am able to find my way around, of course), but I was thinking that perhaps some kind of a conditional could work here if it is indeed possible for a Theme to detect the version of WordPress that's been installed.

    So, is this possible, or am I just sawing logs whille cutting down the forest in Dreamland again? (Furthermore, if it can indeed be done, is it possible to make it PHP 4 and PHP 5 compatible, for those who are stuck on hosting providers using PHP 4 and are either unwilling, unable, or too afraid to move to a new hosting provider?)

  2. #2
    SitePoint Addict
    Join Date
    Jul 2004
    Location
    Salem, OR
    Posts
    272
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dan

    You can call:

    Code:
    <?php echo($GLOBALS['wp_version']); ?>
    This would, in my version print out

    Code:
    2.5.1
    I do not know if this is the BEST way of doing it, but it will get the version info you would want.

    To make a simple 'flow' decision decision based on this version number you could try something like this:

    Code:
    <?php
         if(floatval($GLOBALS['wp_version']) < 2.5.1){
              // Do something because the current version of WordPress is less than 2.5.1
         }else{
              // Do something because the current version of WordPress is, or is greater than 2.5.1
         }
    ?>
    Does that make sense?

  3. #3
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by affordablemagic View Post
    Code:
    <?php
         if(floatval($GLOBALS['wp_version']) < 2.5.1){
              // Do something because the current version of WordPress is less than 2.5.1
         }else{
              // Do something because the current version of WordPress is, or is greater than 2.5.1
         }
    ?>
    That won't work...

    PHP Code:
    if ( version_compare$GLOBALS['wp_version'], '2.5''>=' ) ) {
        
    // Wordpress 2.5 feature
    } else {
        
    // Old Wordpress

    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  4. #4
    SitePoint Addict
    Join Date
    Jul 2004
    Location
    Salem, OR
    Posts
    272
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    logic_earth

    Good catch. Time for sleep.

  5. #5
    In memoriam gold trophysilver trophybronze trophy Dan Schulz's Avatar
    Join Date
    May 2006
    Location
    Aurora, Illinois
    Posts
    15,476
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Cool

    Quote Originally Posted by logic_earth View Post
    PHP Code:
    if ( version_compare$GLOBALS['wp_version'], '2.5''>=' ) ) {
        
    // Wordpress 2.5 feature
    } else {
        
    // Old Wordpress

    So it is possible. Thanks, logic_earth. And thanks for the sample code as well. Since the version checking is being limited to providing new features for WordPress 2.5.x and higher, I don't think I'll be neeeding the else{ } part of the conditional, so I'll probably axe that.

  6. #6
    SitePoint Addict
    Join Date
    Mar 2006
    Posts
    367
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dan,

    It might make more sense to just run an if(function_exists('the_gravatar')) before calling the gravatar function rather than comparing versions.

  7. #7
    In memoriam gold trophysilver trophybronze trophy Dan Schulz's Avatar
    Join Date
    May 2006
    Location
    Aurora, Illinois
    Posts
    15,476
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok Aaron, how would you suggest going about doing that? (It's not like WordPress' code is actually meaningful - it looks a lot like 1999 style tag soup to me.)

  8. #8
    SitePoint Addict
    Join Date
    Mar 2006
    Posts
    367
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    if(function_exists('get_avatar')){
     echo 
    get_avatar($email);
    } else {
     
    //alternate avatar code

    EDIT: Here's a better example straight from WordPress
    http://codex.wordpress.org/Using_Gra..._Compatibility
    Last edited by aaronjj; May 23, 2008 at 12:54. Reason: add link to codex

  9. #9
    In memoriam gold trophysilver trophybronze trophy Dan Schulz's Avatar
    Join Date
    May 2006
    Location
    Aurora, Illinois
    Posts
    15,476
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the link, Aaron. Didn't even think they'd have it up on the Codex for some reason. *facepalms self*


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •