WP-CLI or WordPress Command Line Interface, allows you to manage your WordPress website(s)directly from the command line. With this tool, you can efficiently maintain your WordPress website(s) without a web-browser. For advanced users, WP-CLI can be used to easily install plugins, manage users, configure multisite installations, and much more. These global parameters have the same behavior across all commands and affect how WP-CLI interacts with WordPress. Command documentation is regenerated at every release. To add or update an example, please submit a pull request against the corresponding part of the codebase.
Imagine Peter Parker staring up at a brick wall.
Sure, he could climb that facade like your average disaffected high school student. All it would take is a harness, Mary Jane on belay, and a whole lotta heaving.
Or . . .
He could strip down to his Spidey suit and one-shot web sling his way to the top.
The average WordPress user is a bit like Peter Parker. With some time and work, it’s possible to get your website to do exactly what you want it to just by using the platform’s simple admin dashboard.
But if you’re a power user — admit it, you’re wearing stretchy pants right now — you can sling your way past a variety of time-consuming tasks that crop up because of WordPress’s intentionally simple interface.
Your web?
WP-CLI, which turns tedious tasks into short commands that take mere seconds to execute.
WP-CLI gives you direct access to WordPress capabilities using the command line. This is helpful during regular development and also makes server automation possible. Automation is particularly helpful if you manage more than one or two WordPress installations.
Wp-cli Command User Role
In this article, I’ll first explain how a CLI works and then introduce a few ways WP-CLI can improve your own development workflow. Finally, let’s talk through some of the most fundamental tasks WP-CLI makes possible.
Remember, Peter, with great power comes a great website.
Related: The Difference Between WordPress.com & WordPress.org
What Is a Command Line Interface (CLI)?
The WordPress administrative dashboard is an example of a Graphical User Interface (GUI). You are able to interact with visual elements to execute technical commands, which are then generated and handled behind the scenes.
Then there’s the command line.
This gives you more direct access to your files and database. However, using the command line means you’ll need to better understand what’s happening on your server behind the scenes, and how to trigger certain tasks using special commands. If you enter the wrong command, you could irreparably delete all of your files (remember, good backups are everyone’s business)!
The console is the program that gives you access to the command line. Every computer (and by extension, every server) has a console program installed. It is one of the oldest computer programs and used to be the primary way you could interact with a computer’s capabilities.
To use the command line on your local computer, you can simply open up your pre-installed console for issuing commands:
- For Mac and Linux users, this is called the Terminal. On Windows, look for Command Prompt (and keep in mind that you’ll also need to install an SSH client).
- If you want to access the command line for a remote environment, such as your website’s hosting server, you’ll need SSH credentials provided by the server. You can then access your local command line tool to use SSH and log in remotely.
While the command line is present on every computer, not all web hosting providers allow their users access to remotely log in. You will need to check with your own hosting provider, but these are three of the most common scenarios:
- SSH and CLI access is disabled entirely. WP-CLI will not be available to you.
- SSH and CLI access is available but limits you to using certain commands. WP-CLI may or may not be included, and may be present but with certain limitations.
- SSH and CLI access is available without any restrictions. If WP-CLI isn’t pre-installed, you can easily install it yourself.
If you can’t tell what is available to you by reading the literature on your hosting company’s website, contact technical support to determine which of the three categories they fall into. If your hosting provider falls into the second category, contact them to discover whether WP-CLI is offered and what limitations are present (if any).
WP-CLI + DreamHost
Our automatic updates and strong security defenses take server management off your hands so you can focus on running a great WordPress website.
Introducing WP-CLI
WP-CLI is an open-source project recently brought into the fold of official WordPress.org development projects. The project name is an abbreviation for ‘WordPress Command Line Interface’. The tool is a framework for working with WordPress from the command line and includes a series of commands for common tasks.
If you are more than a casual WordPress user, WP-CLI can significantly reduce the amount of time you spend managing your WordPress website(s). There are a few reasons for this. First, typing is nearly always faster than attempting to click your way through a graphical interface. Once you identify which commands you need, you can quickly copy and paste in everything at once and watch your WordPress sites practically manage themselves.
Then, once you get the hang of WP-CLI, you can enter into the wonderful world of automation. You can write short scripts that invoke WP-CLI to back up and update your sites on a regular basis. These could be run via a cron job daily, weekly, monthly, or at any other interval you choose.
Finally, there are also useful commands to help you circumvent issues that are otherwise nearly insurmountable using the regular interface. For example, what happens if an admin has lost their password, and their email is also no longer current? Normally this would be difficult to resolve, but with WP-CLI, you can easily update their email and password to restore access.
Since WP-CLI is open source, it is completely free.
And DreamHost offers SSH and the full spectrum of WP-CLI to help you manage your WordPress installation from the command line. You’ll simply need to ensure your account has SSH activated, and get it set up for use with your local computer.
WP-CLI comes with many commands that enable you to run most of your WordPress website from your command line. A few of the major options include:
- wp core for managing WordPress core files
- wp comment for regulating comments
- wp db for interacting with the database
- wp export for exporting your content
- wp media for importing or regenerating media
- wp option for updating options
- wp post for managing your posts
- wp scaffold for generating standard code registering post types, taxonomies, plugins, and more
- wp theme for managing your themes
- wp user to control your user list
- wp doctor for diagnosing problems
- wp profile for identifying performance issues
Now that you’re up to speed on WP-CLI and have gotten a glimpse of its possibilities, let’s take a look at different ways you can use it in the real world.
Related: How Much Does It Actually Cost to Set Up a WordPress Website?
Aside from simply being faster to use for general site management, automation is one of the biggest advantages of using WP-CLI. For example, you can automatically push updates to all your client sites using WP-CLI. Linking multiple sites to WP-CLI this way enables you to automatically update WordPress core files, themes, and plugins. What’s more, all of this is possible with a single command (which we’ll discuss in more detail in a later section)!
At this point, you should have a feel for what WP-CLI is and how you’ll be able to use it to supercharge your WordPress development.
Now let’s get your spider-senses tingling (ew!) with some of the fundamental commands.
8 Fundamental Commands for Using WP-CLI Effectively
WP-CLI is capable of handling many tasks, and you can write your very own! However, there are a few fundamentals you can use right away that will immediately save you time when managing WordPress on your server.
Wp Cli Commands
We’ll show you how to complete several key tasks. While these are only the tip of the iceberg, mastering these tasks using WP-CLI will help you save many hours spent managing the basics of your WordPress website.
Plus, they will serve as an interactive way for you to get comfortable with using WP-CLI on a regular basis.
1. Ensure That WP-CLI Is Set Up On Your Server
Checking for and installing WP-CLI on your web server will require SSH. I’ll assume you are already set up to use SSH to access your server (as we discussed previously). Of course, if you’re a DreamHost customer, you can skip this step and be assured that WP-CLI is ready to go.
Before doing anything else, you should determine whether or not WP-CLI is already installed for you. Here are the steps you can take to find this out:
- Log in to your server with SSH.
- Enter the command wp –info
- If you see ‘Command not found’, WP-CLI is not installed.
If WP-CLI is already installed, you can skip the rest of this section and get started with your first commands. Otherwise, follow these steps to install WP-CLI on your server. We’ll show you the recommended method for installation.
First, make sure your server meets the minimum requirements for WP-CLI. This means your environment needs to match the following versions (or have a higher version) for each:
- UNIX-like environment (OS X, Linux, FreeBSD, Cygwin); limited support in Windows environment.
- PHP 5.3.29 or later, which can be found using the php -v command on your server over SSH.
- WordPress 3.7 or later. This can be found in the lower right-hand corner of your WordPress admin.
If you’re not sure whether you are running on UNIX, your hosting provider’s support team should be able to help. Assuming you meet these requirements, it’s time to download the wp-cli.phar file.
Use your command line program to enter the following curl command:
Next, we’ll need to verify that the Phar file is working as expected, using the php command:
Finally, to make the WP-CLI command available as wp, you’ll need to make the file executable and move it to where your server will recognize it as a default command. This is done using chmod and mv, though the specific locations may vary depending on your server setup:
At this point, you should be able to use the wp command like normal. You can test it by running the official wp command:
If everything is working, you’ll get a message similar to this:
Now that WP-CLI is installed, you can start using it in your WordPress development process. You can also install it the same way on your computer, by logging out of SSH and repeating the process within your local development environment.
2. Start a New WordPress Site
If you develop for WordPress, you’ve likely set up many new installations of it on your local computer. With WP-CLI, you can turn WordPress’ famous 5-minute installation into practically a 5-second installation using wp core.
Normally, you would need to load up WordPress in your browser and follow a few pages of prompts. Again, for your average user this is not a problem. It’s when you need to repeat this process many times over for development purposes that it becomes tiresome. Instead, you can simply run an installation using the provided wp core command to download and set up WordPress.
First, download the WordPress files into your server folders:
Next, set up WordPress’ connection to your database with wp config, modifying the parameters to match your database details:
Finally, modify the install command to include the proper settings for your new WordPress site:
That’s it! You’ve now downloaded and installed a brand new WordPress site. It took three lines of code and zero clicking around the browser.
On the other hand, if you’re using DreamHost you can easily create a new WordPress site using your control panel. That way, we’re able to manage the installation process for you, ensuring that it goes as smoothly as possible.
3. Install Updates to WordPress Core
If you already run a WordPress website, you can still benefit from WP-CLI right away. Keeping WordPress core up to date is important for security, and for compatibility with the latest themes and plugins.
This process uses the wp core command as well, to update both the files and database of your WordPress site:
The first command updates all of your files, and the second runs any necessary updates to the database. If you’re running your site on a DreamHost plan such as DreamPress, however, you don’t need to worry about this particular command. We’ll automatically upgrade your site when a new major version of WordPress comes out, so it will stay up to date.
You can use a command to force a downgrade as well. If you upgraded and later ran into a compatibility issue with a vital plugin on your website, you could downgrade using this command:
Slack chicago office. This way, you can always access any version of WordPress you need to at any time. This also makes testing for version compatibility a lot quicker. Of course, it’s always best to try and fix the plugin issue before resorting to downgrading it, since an older version of a plugin can introduce potential security issues.
4. Set Up Themes
You can also install and activate themes using WP-CLI. Typically, you would need to log into your admin and visit Appearance > Themes and manage your desired themes from there. However, you can type in a few short commands to review and choose whichever theme you need.
First, you can check which themes are currently on your WordPress website with wp theme list:
Next, you can use wp theme search <term> to find your desired theme:
Then, you can install and activate your desired theme based on its slug:
With that, you’re all set! These few commands will get your site’s theme up and running.
Want a Free Theme?
When you host with DreamHost you get access to our WP Website Builder tool and 200+ industry-specific starter sites for free!
5. Add, Delete, Activate, and Deactivate Plugins
Not only can you set up themes with WP-CLI, but you can manage your WordPress site’s plugins. This can come in handy for adding new plugins, deleting problematic plugins, or even activating and deactivating plugins. You would normally need to manage these tasks using the WordPress dashboard plugin section.
The commands for managing plugins are similar to those for themes. For a start, you can review currently installed plugins with wp plugin list:
You can also search for new plugins using wp plugin search <term>:
Once you’ve found the plugin you’re looking for, you can install, activate, and deactivate at will:
Additionally, you can update all your plugins at once:
With these commands, you’ll be able to fly through plugin management and not waste any time waiting for browser buffering or dealing with errors within PHP.
6. Manage Your Content With WP-CLI
If you’re in a pinch, you can even make content changes using WP-CLI. You can add, delete, and update posts, all from the command line.
Use the wp post list command to create a list of your existing posts:
You can reference the relevant ID number to update a post using wp post edit <id>:
This will open up a text editor within your command line program that enables you to edit your content. Save and close the file to push the changes to your WordPress site.
Alternatively, you can create a new post altogether, using wp post create:
The final edit flag will again open a text editor where you can easily write and save your post. The post_status flag can be any of the registered post statuses, such as publish, draft, or pending.
To delete a post, use the delete command and the post’s ID number:
While the user interface may provide more control over the visual presentation of a post, this is a great approach for those who prefer the minimalist environment of a plain text editor.
7. Run Nearly Any Database Query With WP-CLI
For more advanced developers, it might be necessary to examine custom database queries or test existing queries for efficiency. Whatever the reason, if you are interested in creating or editing database queries, WP-CLI enables you to write them directly in the command line.
Typically, this is not possible with a normal WordPress installation. You would need special database plugins or additional GUI access to the database through your hosting provider.
If you want to access the regular MySQL command line interface using your WordPress credentials, use the wp db cli command:
However, you can also execute MySQL queries directly from WP-CLI. These queries can be made using the wp db query command. For example, let’s say you want to select all users so you can see their user logins and ID numbers:
This will return a table with the selected information for your users. You can grab one of the author IDs, and then list all the posts that author has written:
Which MySQL commands you’ll need to run will depend on the work you’re doing, but you can clearly see how easy WP-CLI makes it to interact with your database.
8. Access the WP-CLI Help Manual for Guidance
As you may have realized by now, WP-CLI comes packed with a huge amount of power through its many commands. It can become a little difficult to keep up! Thankfully, the tool includes a robust help system.
You can use the wp help command to access support at any time:
Typing this in will provide you with a complete list of command options. You can add the command you need information for at the end to get help for it specifically. For example, let’s say you need to know more about the wp plugin command:
This will provide you an in-depth information page about using this command. You can do this for each subcommand as well. For example, the wp plugin install command has its own help manual:
Each built-in WP-CLI setup has this feature, so you can explore its power at your own leisure. You can also find much of the same information and support on the WordPress.org Developer Resources website.
Ready to Build Your WordPress Site?
Whether you need help choosing a blogging platform, understanding WordPress hosting, or picking an awesome free theme, we can help! Subscribe to our monthly digest so you never miss an article.
Master and Commander
In the end, WP-CLI is a powerful tool for managing WordPress sites efficiently, simplifying many tasks that otherwise take a lot of time to execute manually using the GUI. This is particularly helpful for you power users out there working with multiple installations of WordPress at once.
What questions do you have about getting started with WP-CLI? Join the DreamHost community and start the conversation!
Register for just $2.99
Normally $49.95
Register NowRelated Articles
If there’s one thing developers love, it’s finding ways to speed up and automate boring, manual tasks for WooCommerce development. After all, that’s one of the reasons why we built Robot Ninja.
And I’m sure a lot of developers would agree with me that WP-CLI is one of the best inventions since sliced bread. It’s my favorite way to set up new WordPress sites, work with WooCommerce, manage plugins, test code, and a whole bunch of other tasks that are just so much quicker to do via command line.
Even better, WooCommerce CLI (WC-CLI) allows developers to tackle tasks in WooCommerce development more efficiently. Want to create a new product for your store? You can get it done with a single command, which is faster than clicking around the WordPress admin.
In this post, I’ll run you through how WP-CLI and WC-CLI work and give you a rundown of my go-to commands for spinning up new WordPress sites and WooCommerce development.
What is WP-CLI?
For a long time, WordPress didn’t have a way for developers to work around simple tasks that would take just a few lines of code. But that changed when WP-CLI came along in 2009.
If you’re new to the magic of WP-CLI, here’s how it’s described on the official website:
WordPress is an easy blogging platform that is being used by millions of bloggers worldwide and which releases thousands of themes and plugins carefully crafted by skilled developers. Here at Themes.to our mission is to share with you the best free WordPress Themes and Plugins that, apart from making your site looks great and works Perfectly. Free wordpress themes 2020 calendar. Create WordPress Website with the best free WordPress Themes. 7000+ Web Designs. Premium, responsive, fully customizable with easy Drag-n-Drop editor. Blog and Portfolio themes.
“WP-CLI is a set of command-line tools for managing WordPress installations. You can update plugins, set up multisite installs and much more, without using a web browser.”
Originally developed by Andreas Creten and Cristi Burcă, the project was maintained by Daniel Bachhuber until it was picked up as the official WordPress command line tool under the WordPress.org umbrella in December 2016.
When WordPress co-founder Matt Mullenweg announced WordPress’ support for the project, he called WP-CLI “one of the highest impact developments for WP in many years.”
WP-CLI and WC-CLI
While WP-CLI is a set of command-line tools for managing WordPress installations, the WooCommerce CLI (WC-CLI) layer adds tools for managing products, coupons, payment gateways, shipping zones, and more.
WooCommerce support for WP-CLI was introduced way back in 2.5 and there have been a ton of improvements since then. In versions 2.5 and 2.6, the CLI was powered by its own code separate from the REST API or WooCommerce core, which meant code could end up being duplicated across the code base. It also meant certain things possible in the REST API were not possible at all with the CLI.
WooCommerce 2.7 was a game changer—it introduced a new CLI powered by the REST API by forking RESTful. So most of what is possible with the REST API can also be achieved via command-line.
The full list of available WC-CLI commands (as of WooCommerce 3.0) are listed on the WC-CLI wiki page, which contains more information and examples.
Using WC-CLI for Development
Here’s a quick example to show you just how powerful—and time-saving—WC-CLI can be.
Imagine you’re creating a new online store for a client and you have a huge list of products to add, which are in CSV format.
If you were to set everything up without WP-CLI and WC-CLI, you would have to:
- Download and install WordPress manually (which doesn’t take 5 minutes, despite the “famous” 5-minute install!) – 10 minutes
- Install and activate plugins, one-by-one (including WooCommerce) and your theme – 10 minutes
- Create every single product in the CSV manually, or import them with a plugin – could take days
The same process with WP-CLI and WC-CLI:
- Download and install WordPress via command line – 2 minutes
- Install and activate plugins (including WooCommerce) and theme via command line – 2 minutes
- Create product via command line – this would still take a long time, but not days.
The fact is, computers are faster than humans. And who wants to spend all day clicking around WordPress anyway?
Getting Started with WP-CLI
Before you can start playing around with WC-CLI, you need to set up WP-CLI, making sure your hosting environment meets the basic requirements:
- A UNIX-like environment.
- PHP 5.3.29 (or later).
- WordPress 3.7 (or later).
- Secure Shell (SSH) access to your hosting environment.
Obviously, you need access to the command line, but not all hosts provide it. So get in touch with your host to find out if it’s available. Most (if not all?) managed WordPress hosts have support WP-CLI, including:
I won’t go into detail about how to install and set-up WP-CLI here. Instead, I highly recommend you check out the installation guide on the WP-CLI site.
Core and Community WP-CLI Commands
There are two main classes of WP-CLI commands:
- Core commands – The core list of WP-CLI commands.
- WP-CLI packages – Community maintained commands.
- Bundled commands – Commands that are bundled with plugins.
A quick look through the huge list of community commands could save you a bunch of time, with all kinds of commands for day-to-day tasks like security checks, deployment and migration assistant tools, search and replace (great for syncing test and production sites), database checkpoints, and more.
One of the best things about WP-CLI is that you’re not limited to the core commands—you can define your own custom commands (I’ve created my own presets—more on that later), which is as simple as writing your own PHP class. The commands cookbook sets out how to write your own commands.
For a non-exhaustive list of commands available in plugins, check out the Tools page on the WP-CLI site, which lists all kinds of commands for popular and lesser-known plugins.
WP-CLI Commands for WordPress Development
Everything you can do via the WordPress admin can be done via command line for WooCommerce development. Here are my go-to WP-CLI commands.
Setting Up a New WordPress Install
Let’s start with the basics. To download and extract the latest version of WordPress to the current directory:
View the code on Gist.
To create the wp-config.php file and configure the database settings:
View the code on Gist.
Finally, to run the install command to set up the database:
View the code on Gist.
wp db <command>
This command is for basic database operations using credentials stored in wp-config.php. It’s by far the command I use the most for quickly making a database backup/export. You can read more about how to use it over on the WordPress Developer Handbook site.
Exporting Database
Exporting your database is easy and the command is relatively short. I usually run this quick command before making updates, or when testing WooCommerce features locally in case I want to revert the site back to a specific state from before I started tinkering with it.
View the code on Gist.
Restoring Database
When I want to revert a site back to a specific state, this is the command:
View the code on Gist.
Other Useful Commands
To display the size of the tables in the database:
To display the size of the tables in the database:
View the code on Gist.
To quickly optimize the database:
View the code on Gist.
To repair the database:
View the code on Gist.
Generating Posts
The wp post generate command is useful for creating posts with dummy content for test sites.
View the code on Gist.
Search and Replace
To search and replace all the strings in the database, the WP-CLI team have created a handy package, which you can download from GitHub.
This command searches through all rows in a selection of tables and replaces appearances of the first string with the second string:
View the code on Gist.
It handles PHP serialized data and doesn’t change primary key values.
Local Development with Laravel Valet
If you use Laravel Valet, this package is useful for setting up a local development environment.
This command will spin up a new WordPress installation—complete with database and HTTPS:
View the code on Gist.
Saving Time with WP-CLI Presets
Trying to remember every single WP-CLI command can be painful at times—it takes 3 commands just to install WordPress. So I created my own personal presets, which run through some common actions I do day-to-day in WooCommerce development.
You can check out my presets here on GitHub.
Basically, it’s a wrapper for a bunch of other WP-CLI commands that I use locally to make the process of spinning up new sites a little quicker. For example:
View the code on Gist.
Or, to clone WooCommerce, Subscriptions, Stripe etc:
View the code on Gist.
Do you have any time-saving tips for WP-CLI? I’d love to hear if you have similarly created your own personalized commands.
WC-CLI Commands for WooCommerce Development
Like WP-CLI, everything you can do in the WordPress admin during WooCommerce development can also be done via command line, such as:
- Creating products (or importing them from a file)
- Updating/deleting products and orders
- Creating/updating customer data, passwords, coupons
- Reviewing products
- and a whole lot more
You can quickly tell a WC-CLI command from a WP-CLI—it’ll be prefaced by a “wc” rather than a “wp.”
For a full list of WC-CLI commands, check out the WC-CLI wiki page.
Creating Products
With WC-CLI, you can create all the default types of products that you can create via the WordPress admin, i.e. simple, variable, grouped, and external products.
WooCommerce Happiness Engineer Nicola Mustone has previously published a fantastic post about managing products with WP-CLI, which I highly recommend checking out. Here are the basics:
Simple Products
This command would create a simple product with title Test Product, a SKU WCCLITESTP and a price of $20.00.
This command would create a simple product with title Test Product, a SKU WCCLITESTP and a price of $20.00.
View the code on Gist.
External Products
Creating external products is similar to the command above, but you need to also specify the product URL and the button text. These are options, but it’s a good idea to enter the product URL so you don’t have to manually do it later.
Creating external products is similar to the command above, but you need to also specify the product URL and the button text. These are options, but it’s a good idea to enter the product URL so you don’t have to manually do it later.
Add these parameters to the command above for simple products to specify the URL and the button text:
View the code on Gist.
Also, make sure to change the product type to external with
--type=external
.For more on grouped products and variable products, check out Nicola’s post.
Creating Bulk Products
When creating bulk products for testing, I’ve found WC Cyclone really useful. WC Cyclone is a WordPress plugin that adds several WP-CLI commands for generating fake WooCommerce data.
For example, to generate a bunch of products, you would use this command:
View the code on Gist.
By default, the product type is
books
but can also be food
.Getting a List of Orders
Want to see a list of orders? This command is useful for quickly viewing orders on your development site and for when you’re testing:
View the code on Gist.
I’d recommend specifying the fields you want to see to avoid a lot of data appearing in your terminal.
Getting a List of Customers
Similar to the command above, this one gives you a list of customers:
View the code on Gist.
Again, I highly recommend specifying the fields you want to see.
Both this command and the one above for getting a list of orders are commands I rely on heavily during development. They save me so much time—I don’t have to click through the WordPress admin to view certain variables, especially if I find I’m repeating tests after making a lot of changes.
WooCommerce Extensions That Work with WP-CLI
As I mentioned earlier, a lot of plugins come with bundled WP-CLI commands, and WooCommerce extensions are no exception.
WooCommerce Memberships (a SkyVerge product) and WooCommerce Subscriptions (a Prospress product, like Robot Ninja) are two extensions I use a lot. Here are some useful commands that come with these extensions:
WooCommerce Memberships
Managing WooCommerce Memberships plans and members is quick and easy via command line. This is the primary command for this extension:
View the code on Gist.
To create an unlimited membership for user 1 to plan 100:
View the code on Gist.
This command creates an expired silver membership for user 1 that started on Jan 1, 2016, and ended on June 30, 2016:
View the code on Gist.
When you’re developing locally and repeating these functions a lot, having the ability to copy and paste these commands rather than click through the WordPress dashboard is a huge time-saver.
Combining a bunch of the common commands you run into a script, or something like the presets commands I mentioned earlier, can really help with automating tedious, repetitive commands.
For a detailed rundown of all the WP-CLI commands for WooCommerce Memberships, check out the documentation.
WooCommerce Subscriptions + Action Scheduler 2.0
We’ve recently released WooCommerce Subscriptions 2.3 and with it version 2.0 of Action Scheduler ?.
Action Scheduler is a robust job queue and background processing library for use in WordPress plugins and WooCommerce extensions. Version 2.0 also comes with some new WP-CLI commands for running scheduled actions. This command provides an important new way to run scheduled actions, especially for high-volume stores.
Whilst the Action Scheduler WP-CLI commands aren’t specifically for development, they are really useful for developers/site admins who want optimal performance for running scheduled actions.
WooCommerce Subscriptions uses Action Scheduler to handle triggering actions, such as subscription renewals on a recurring or one-off basis.
Out-of-the-box, Action Scheduler makes use of the default WP Cron runner to trigger these actions. For many sites, specifically sites with large numbers of actions, relying on WP Cron can become a performance hazard. Using the WP CLI commands you can now speed up these actions and do cools things like create queues to process only actions in a specific group, or with a specific hook or hooks. When processing large batches of actions concurrently, making each queue handle a specific action can greatly increase the rate of processing and reduce any potential for issues like database deadlocks.
Common cases where WP-CLI is a better option include:
- Long-running tasks – Tasks that take a significant amount of time to run.
- Large queues – A large number of tasks will naturally take a longer time.
- Other plugins with extensive WP Cron usage – WP Cron’s limited resources are spread across more tasks.
With a regular web request, you may have to deal with script timeouts enforced by hosts, or other restraints that make it more challenging to run Action Scheduler tasks. Using WP CLI to run commands directly on the server gives you more freedom, and means that you typically don’t have the same constraints of a normal web request.
There are a number of WP-CLI commands and options available. The best way to get a full list of commands and their available options is to use WP-CLI itself. This can be done by running
wp action-scheduler
to list all Action Scheduler commands, or by including the --help
flag with any of the individual commands. This will provide all relevant parameters and flags for the command.Further Reading
We’ve barely scratched the surface of what WP-CLI and WC-CLI can do in this post. For WooCommerce development, I highly encourage you to check out this fantastic tool. Here are some resources to help get you up to speed:
- WP-CLI website (wp-cli.org) – Learn more about the official command line interface for WordPress.
- WP-CLI handbook (wordpress.org) – The official WordPress developer handbook entry.
- WP-CLI Commands – Full list of WP-CLI commands.
- WC-CLI Commands – Full list of WC-CLI commands.
- WooCommerce WP-CLI Basics – Automattic Engineer Rémi Corson put together this excellent presentation on the basics for a WordPress Meetup.
- Managing Products with WooCommerce CLI – WooCommerce Happiness Engineer Nicola Mustone has published some handy commands for WooCommerce development via command line.
- Bulk Import WooCommerce Products with WP-CLI & Alfred From a Text File – A straightforward guide also put together by Rémi Corson. His blog is a good one to bookmark for other WooCommerce development tutorials.
Conclusion
If you’re like me, you enjoy the thrill of fixing problems, building things, and losing yourself in lines of code. But there’s only so much time in the day. You’ve probably got better things to do than the same boring tasks over and over.
WP-CLI is such an amazing, handy tool. As I’ve talked about in this post, it’s my go-to for everything from setting up local installs of WordPress to managing WooCommerce products. Hopefully, this overview has given you a good basis for understanding how WP-CLI and WC-CLI work and how you can use the command line to speed up your own WooCommerce development.
If you’re interested in learning more about how we develop with WooCommerce, check out WooCommerce Developer’s Toolbox: 10 Tools We Use at Robot Ninja.
Do you use WP-CLI? Got any questions about using WP-CLI and WC-CLI? Let us know in the comments below.