SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Addict beebs93's Avatar
    Join Date
    Jul 2010
    Location
    Vancouver, Canada
    Posts
    207
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How would you test for a WP environment?

    I am making a simple PHP library for my own personal toolbox which involves caching variables on the server.

    If the library is being run on a WP site it will use the Transient API instead of the APC extension to cache the data.

    As it stands, my litmus test to see if it's a WP environment is to check if get_the_permalink() function exists and to see if the constant WP_CONTENT_DIR has been defined.

    I feel this is a bit hacky and would like to know if there is a better way to go about this.

    Cheers!
    "To be truly dedicated to something
    you must be willing to betray it.
    " -SW

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    extension_loaded(), or if you there are more dependencies check through the array get_loaded_extensions();

    HTH

  3. #3
    SitePoint Addict beebs93's Avatar
    Join Date
    Jul 2010
    Location
    Vancouver, Canada
    Posts
    207
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I use extension_loaded to check for APC already, but how can I use that to test for a WP environment?
    "To be truly dedicated to something
    you must be willing to betray it.
    " -SW

  4. #4
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Your current solution sounds like a valid way of checking if your code is called in the context Wordpress to me.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  5. #5
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    61 Post(s)
    Tagged
    0 Thread(s)
    If you're only really after using the Transients API, why not take look to see if that is available?

    PHP Code:
    if (function_exists('set_transient')) { 
        
    $function = new ReflectionFunction('set_transient'); 
        
    $phpdoc   $function->getDocComment(); 
        if (
    strpos($phpdoc'@package WordPress') && strpos($phpdoc'@subpackage Transient')) { 
            
    // Do something to record that Transients API is available 
            
    echo $phpdoc;
        } 

    Salathe
    Software Developer and PHP Manual Author.

  6. #6
    SitePoint Addict beebs93's Avatar
    Join Date
    Jul 2010
    Location
    Vancouver, Canada
    Posts
    207
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Anthony & Salathe - I appreciate the help
    "To be truly dedicated to something
    you must be willing to betray it.
    " -SW


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
  •