I use the wonderful WP-CLI tool. Due to dependence on Apache environment variables for a specific use case, I need to enable a bit of code to run only when running under WP-CLI. How can I detect if WP is running under WP-CLI?

In this specific case I could check for the presence of the Apache environment variables in question. However, I would like to know the more general, canonical method to check. Thank you.

2

Within the php/wp-cli.php we find these lines:

// Can be used by plugins/themes to check if WP-CLI is running or not
define( 'WP_CLI', true );
define( 'WP_CLI_VERSION', trim( file_get_contents( WP_CLI_ROOT . '/VERSION' ) ) );
define( 'WP_CLI_START_MICROTIME', microtime( true ) );

so you could check if WP_CLI or WP_CLI_VERSION are defined.

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *