Anybody who has used Command Line will know how brilliant it can be. If you know the right commands you can do pretty much anything faster than you could with a GUI, albeit with possibly disastrous ramifications if done incorrectly, but surely that is where half the fun lies!

What is the WordPress Command Line?

As of WordPress 3.7 and later (rather old now!) there has been the WordPress Command Line, WP-CLI for short. With the introduction of WP-CLI came a faster way to do the basics, through to the most advanced functions all from the comfort of your Terminal.

I won’t go over the installation of WP-CLI here, there is a nice guide for all the basics over at https://wp-cli.org/

As a standard with WP-CLI you can change almost everything within WordPress core. There are functions for everything ranging from creating a new blog post, to updating WordPress and any Plugins, all the way to exporting the Database. When you first look into WP-CLI it can all appear rather daunting.

As all developers should be, I was eager to improve my knowledge and wanted to try to conquer WP-CLI, if you are the same, or even slightly curious about WP-CLI then I would 100% recommend playing around with it on a local development site. After playing around with it for a while I decided to try to implement WP-CLI into our most successful plugin, Ultimate Under Construction.

Why should you use the WordPress Command Line?

The WordPress Command Line is a brilliant tool. Do you get annoyed by pesky time consuming updates? If only there was a way to automate updates, or anything else for that matter. Well that is where the WordPress Command Line comes in handy. 

As you get more used to working with the command line and it all becomes second nature, the WP-CLI can be one of the most powerful tools at your disposal. Everything ranging from switching over your current Theme to creating a batch file to run multiple functions concurrently.

We use a variety of batch files, The most useful one being for updates. Using our Under Construction plugin we can now enable the under construction page, check for WP updates, update any required Plugins and Themes all from running one batch file.

Much like with git or any other command line tool the more you use it, the more you discover its power. Without much further ado I will begin teaching you the basics of WordPress Command Line.

A working example of the WordPress Command Line

The first step is to check that WP-CLI is running and works on your version of WordPress, which it should be if you keep your site secure and up to date. Below is the function we used to check for WP-CLI, within this function we are requiring our new WP-CLI classes.

Once that is done the next task is to create our new class. We are going to be extending the WP-CLI class “WP_CLI_Command”. Here is a snippet of code for the class. I will then go through the basic functions we have implemented into the plugin.

The WordPress Command Line class works the same as every other class. Once you have the class you can easily create a new function. These will finish off your WP-CLI command, for example if you have added a new command of “uuc” and then created a function called “enable”, then typing “wp uuc enable” into the command line will run the enable function within your new class extension of the WP-CLI.

Each function can, and probably should, be given instructions, this is done as below. All the useful information within the comment will be displayed when you run the help command “wp help uuc”.

As you can see from the function above, before we are declaring the function we have a few lines. The first line is the purpose of the function. Next we have a list of examples of the function within the command line. Now when you run “wp uuc help” within the command line you will see your new function name under Subcommands along with the purpose of your new function, see the below image for an example.

Screen Shot 2016-05-20 at 14.55.50

Finally, we want our new Enable function to do something useful. In the context of our plugin we want to use our new function to enable the under construction page.

This is done once again, like any other PHP function would be. The above code will be added into the function, this will grab the currently saved settings for the plugin using ‘get_option’. We are then checking if the Under construction page is currently enabled or not. If not we are then enabling the page and updating the options, finally we are returning a message to the command line. You will see ‘WP_CLI::success’. This will return the below success message.

Screen Shot 2016-05-20 at 15.12.18

There are 3 main ways to return an output to the command line. These are ‘success’ which we have already used. ‘Error’ which is fairly self explanatory, and finally ‘log’ which would be used to return a plain string to the console. The image below shows a brief example of each return.

Screen Shot 2016-05-20 at 15.16.01

Learning more

This is a brief example of what you can do with WP-CLI, I will be writing another blog post once Ultimate Under Construction version 2.0 is released, going over the more advanced WP-CLI features we have so stay turned to learn some more!

If you want to learn more about WP-CLI you can take a look at some of the resources I used when writing the above code, below is a list of useful resources.

  • The Post Status Podcast did an excellent episode about the WordPress Command Line.
  • Of course you can always go to wp-cli.org for the entire documentation.
  • The entire project is, as usual, hosted on Github.

Good Stuff? Get more eCommerce News & advice...

Leave a Reply

Your email address will not be published. Required fields are marked *