Confident Clean a Hacked Site Workshop Join me March 21st for this 90 Minute Workshop  Claim your spot

Extending WP-CLI

WordPress


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:');
WP_CLI::success($option);

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

 


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

class Example_Command extends WP_CLI_Command{
        
        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

You?

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.

Helping you and your customers stay safe


WordPress Security Consulting Services

Power Hour Consulting

Want to get expert advice on your site's security? Whether you're dealing with a hacked site or looking to future-proof your security, Tim will provide personalised guidance and answer any questions you may have. A power hour call is an ideal starting place for a project or a way to break deadlocks in complex problems.

Learn more

Site Reviews

Want to feel confident about your site's security and performance? A website review from Tim has got you covered. Using a powerful combination of automated and manual testing to analyse your site for any potential vulnerabilities or performance issues. With a comprehensive report and, importantly, recommendations for each action required.

Learn more

Code Reviews

Is your plugin or theme code secure and performing at its best? Tim provides a comprehensive code review, that combine the power of manual and automated testing, as well as a line-by-line analysis of your code base. With actionable insights, to help you optimise your code's security and performance.

Learn more

Or let's chat about your security?

Book a FREE 20 minute call with me to see how you can improve your WordPress Security.

(No Strings Attached, honest!)