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! :slight_smile:

extension_loaded(), or if you there are more dependencies check through the array get_loaded_extensions();


I use extension_loaded to check for APC already, but how can I use that to test for a WP environment?

Your current solution sounds like a valid way of checking if your code is called in the context Wordpress to me. :slight_smile:

If you’re only really after using the Transients API, why not take look to see if that is available?

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;

Thanks Anthony & Salathe - I appreciate the help :smiley: