How to create WordPress CLI command


Intermediate

WordPress Command Line Interface is a great tool for developer just as it is, but it is even greater as an automation tool if you create your own commands. Here is just simple model how to register new command. Include this to your Functions.php.

What it does is first check if WP_CLI exists. Then write function you want to run. Then add new command using WP_CLI::add_command which takes two parameters. First parameter is your command name and second is function to run. So if you would now run in your terminal at your WordPress root ‘wp update-meta’ it would run that function.

<?php

if(!class_exists('WP_CLI'))return;

function my_function (){  
  // Your code to run comes here
}

WP_CLI::add_command('update-meta', 'my_function');