I want to create a custom WP CLI command and I have read the documentation but I just don't understand for the life of me how this works.

Do I need to declare it before in my config file where they are gonna go?

Is it a PHP file? If so, I need to declare it put it anywhere inside my theme?

Thank you in advance.


1 Answer 1


You can put the PHP anywhere in your plugin, or a file that's included into your plugin, the same as you would any other code for extending any part of WordPress in a plugin. The only thing you need to do is check that WP_CLI exists before using any of the WP CLI API:

if ( defined( 'WP_CLI' ) && WP_CLI ) {
    // WP_CLI::add_command etc.
  • Can I include this as a theme file and call it within functions.php? Commented Nov 28, 2019 at 16:58
  • Yes. It will work in themes too. Commented Nov 28, 2019 at 22:41

Your Answer

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

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