Detect whether WP-CLI is running

The best way to detect whether WP-CLI is running is to inspect the WP_CLI constant.

The best way to detect whether WP-CLI is running is to inspect the WP_CLI constant.

For instance, if you want to load a custom command from your plugin, you might use a conditional like this:

if ( defined( 'WP_CLI' ) && WP_CLI ) {
    require_once dirname( __FILE__ ) . '/command.php';
}

Or, if you need to apply some conditional logic in your wp-config.php, you might do so like this:

if ( defined( 'WP_CLI' ) && WP_CLI ) {
    define( 'DB_USER', 'privilegeduser' );
} else {
    define( 'DB_USER', 'limiteduser' );
}