Wordpress - Detect if WP is running under WP-CLI
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.
The canonical check for WP-CLI, which is used in most plugins and is explicitly mentioned in the docs, is the check whether WP_CLI
is defined and set to true:
if ( defined( 'WP_CLI' ) && WP_CLI ) {
// Do WP-CLI specific things.
}