Dev/Sec/Ops with a splattering of humour

Extending WP-CLI

Really old post – This post is from 3 years, 3 months and has not been updated, some if not all of the information maybe out of date. Proceed with caution.

View this video on YouTube here:

This is the second video in a short series on working with WP-CLI, and over the next few weeks we will be releasing additional videos for both WP-CLI as well as other topics. These video tutorials are possible due to my amazing Patrons through Patreon.

Extending WP-CLI

This tutorial was recorded using a VVV vagrant install which is a virtualised instance of a development server, which comes with WP-CLI installed by default.

In this episode we take a look at using eval-file and creating our own commands through plugins with WP-CLI.

Please note that a couple of the shots in the screencast refer to slightly differently named files on the command line. Through the power of video editing, the screens were recorded separately from the audio so at one point I say our_options.php but the screen shows siteurl.php. Don’t panic, this is because I have two files, one with the simple example and the second using parameters.

So from the video our eval-file example:

wp eval-file scripts/our_option.php siteurl

The full file looks like:

$option = false;

if(!empty($args) && isset($args[0]) ){
        $option = get_option($args[0]);

if( !$option ){
        WP_CLI::error('No Argument Specified');

WP_CLI::line('You requested:');

This is example code only, obviously not for use on a live site, and WP-CLI has a get option command built in which we used in the first episode.

In a similar way our final example plugin, wp-cli-example.php

Plugin Name: Example WP-CLI
Version: 1.0
Description: Demo for WP-CLI Tutorial
Author: Tim Nash
Author URI: https://timnash.co.uk

if( defined( 'WP_CLI' ) && WP_CLI ) {
WP_CLI::add_command( 'example', 'Example_Command' );

class Example_Command extends WP_CLI_Command{
        * @subcommand get-option
        * @alias option
        function get_option( $args, $assoc_args ) {
                $default = 'example.com';
                if( $assoc_args[ 'default' ] ){
                        $default = $assoc_args[ 'default' ];
                $return = get_option( $args[0], $default );
                WP_CLI::success( $return );


In this scenario you could run:

wp example command siteurl default=="http://example.com"

Once again this is just sample code, and you could have gotten the options back using the built in commands.

So why use any of this stuff?

I tend to use eval-file for quick scripts examples include:

  • Database clean ups
  • Quick and dirty export of data
  • Interrogating the DB
  • Reporting

I have also at times used it within a plugin development workflow to test individual functions or ideas, without including it in the plugin initially.
On the other hand I tend to always write an interface for WP-CLI for my plugins, and often write the WP-CLI commands before writing the admin interface as then I will use it as the primary method for testing and tearing down.

Within plugins, I use WP-CLI in a similar way to the way I use scripts with eval-file, but also to replicate settings page, so if I can set a setting via a settings page, then it should also be possible from the command line.

Coming up next

In our next video we are going to look at Community Packages and some more interesting hints and tips

  • Working with wp-cli.yml files for more advanced config options
  • Remotely controlling WordPress sites through WP-CLI via SSH and using WP-API
  • More complex Shell Scripting and using WP-CLI to automate a lot of your everyday tasks
  • Using and creating community packages

Patreon Patrons


It’s only because of these fine people that videos like this are produced! iIf you want to help me film more video tutorials then please do consider becoming a Patreon Patron. For more information on why I chose to fund these videos through Patreon please see my introduction to Patreon post. Or to jump straight in and become a Patron by donating $1 or $5 – just visit my Patreon page for details.