最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

wp cli - Detect if WP is running under WP-CLI

programmeradmin0浏览0评论

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.

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.

Share Improve this question asked May 10, 2016 at 9:25 dotancohendotancohen 8491 gold badge9 silver badges20 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 40

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.
}
发布评论

评论列表(0)

  1. 暂无评论