admin管理员组文章数量:1122832
Updated::
I am having issues when adding a custom cli command into an existing wordpress theme. When I run wp --info
I get the following:
PHP binary: /usr/local/Cellar/php70/7.0.13_6/bin/php
PHP version: 7.0.13
php.ini used: /usr/local/etc/php/7.0/php.ini
WP-CLI root dir: phar://wp-cli.phar
WP-CLI packages dir:
WP-CLI global config:
WP-CLI project config: /Users/julie/isl-site/isl-site/wp-cli.yml
WP-CLI version: 1.0.0
However, when I register the example command within my theme/functions.php
function foo_command( $args ) {
WP_CLI::success( $args[0] );
}
WP_CLI::add_command( 'foo', 'foo_command' );
I get the following errors:
Uncaught Error: Class 'WP_CLI' not found in /my-site/public/wp-content/themes/my-theme/lib/theme-functions.php:278
Stack trace:
15:23:29 web.1 | #0 /Users/julie/my-site/public/wp-content/themes/my-theme/functions.php(36): require_once()
15:23:29 web.1 | #1 /Users/julie/my-site/public/wp/wp-settings.php(387): include('/Users/julie/...')
15:23:29 web.1 | #2 /Users/julie/my-site/public/wp-config.php(212): require_once('/Users/julie/...')
15:23:29 web.1 | #3 /Users/julie/my-site/public/wp/wp-load.php(44): require_once('/Users/julie/...')
15:23:29 web.1 | #4 /Users/julie/my-site/public/wp/wp-blog-header.php(13): require_once('/Users/julie/...')
15:23:29 web.1 | #5 /Users/julie/my-site/public/index.php(5): require('/Users/julie/...')
15:23:29 web.1 | #6 {main}
15:23:29 web.1 | thrown in /Users/julie/my-site/public/wp-content/themes/my-theme/lib/theme-functions.php on line 278
Any insight on how to resolve this issue?
Updated::
I am having issues when adding a custom cli command into an existing wordpress theme. When I run wp --info
I get the following:
PHP binary: /usr/local/Cellar/php70/7.0.13_6/bin/php
PHP version: 7.0.13
php.ini used: /usr/local/etc/php/7.0/php.ini
WP-CLI root dir: phar://wp-cli.phar
WP-CLI packages dir:
WP-CLI global config:
WP-CLI project config: /Users/julie/isl-site/isl-site/wp-cli.yml
WP-CLI version: 1.0.0
However, when I register the example command within my theme/functions.php
function foo_command( $args ) {
WP_CLI::success( $args[0] );
}
WP_CLI::add_command( 'foo', 'foo_command' );
I get the following errors:
Uncaught Error: Class 'WP_CLI' not found in /my-site/public/wp-content/themes/my-theme/lib/theme-functions.php:278
Stack trace:
15:23:29 web.1 | #0 /Users/julie/my-site/public/wp-content/themes/my-theme/functions.php(36): require_once()
15:23:29 web.1 | #1 /Users/julie/my-site/public/wp/wp-settings.php(387): include('/Users/julie/...')
15:23:29 web.1 | #2 /Users/julie/my-site/public/wp-config.php(212): require_once('/Users/julie/...')
15:23:29 web.1 | #3 /Users/julie/my-site/public/wp/wp-load.php(44): require_once('/Users/julie/...')
15:23:29 web.1 | #4 /Users/julie/my-site/public/wp/wp-blog-header.php(13): require_once('/Users/julie/...')
15:23:29 web.1 | #5 /Users/julie/my-site/public/index.php(5): require('/Users/julie/...')
15:23:29 web.1 | #6 {main}
15:23:29 web.1 | thrown in /Users/julie/my-site/public/wp-content/themes/my-theme/lib/theme-functions.php on line 278
Any insight on how to resolve this issue?
Share Improve this question edited Jan 17, 2017 at 20:30 Julie asked Jan 17, 2017 at 16:39 JulieJulie 411 silver badge3 bronze badges 4 |2 Answers
Reset to default 12You'll need to wrap your statement within a class_exists()
check:
function foo_command( $args ) {
WP_CLI::success( $args[0] );
}
if ( class_exists( 'WP_CLI' ) ) {
WP_CLI::add_command( 'foo', 'foo_command' );
}
This way, the command is only registered when the WP_CLI
class exists — because the WP_CLI
class will only exist when WP-CLI is running.
You can check if WP_CLI
is defined e.g.
function foo_command( $args ) {
WP_CLI::success( $args[0] );
}
if ( defined('WP_CLI') && true === constant('WP_CLI') ) {
WP_CLI::add_command( 'foo', 'foo_command' );
}
本文标签: wp cliWPCLI with theme Uncaught Error Class 39WPCLI39 not found
版权声明:本文标题:wp cli - WP-CLI with theme: Uncaught Error: Class 'WP_CLI' not found 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736308597a1933715.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
wp-cli.yml
file – RobBenz Commented Jan 17, 2017 at 21:06