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?

  • I guess my question is where exactly I need to put the code for WP_CLI::add_command() as I thought I could just put it in a file (in this case theme-wp-cli-commands.php) within the theme.
    – Julie
    Jan 17, 2017 at 19:16
  • Thanks for your prompt responses @RobBenz I am just learning wpi-cli. I did as you suggested and added the following to my functions.php please see the updated question. I think it has to do with my wp-config.php file
    – Julie
    Jan 17, 2017 at 19:59
  • to the best of my knowledge you do not use wp-cli with a theme. it is more of a platform to manage your wordpress install entirely. what exactly is the command you are trying to register.
    – RobBenz
    Jan 17, 2017 at 20:58
  • you could try adding the command you are trying to register to wp-cli.yml file
    – RobBenz
    Jan 17, 2017 at 21:06

1 Answer 1


You'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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.