- Understanding WP-CLI Commands
- Pros and Cons of WP-CLI
- Creating Custom WP-CLI Commands: An In-Depth Look
- Set up a WordPress plugin
- Create a commands folder
- Structure the command class
- Example 1: Clearing All Transients
- Example 2: Listing Custom Post Types
- Frequently Asked Questions
- What is WP-CLI?
- How do I create a custom WP-CLI command?
- Do I need coding skills to use WP-CLI?
- Is WP-CLI safe to use on a live site?
- Conclusion
Quick Answer
WP-CLI is WordPress’s command line interface, and custom WP-CLI commands let you automate tasks specific to your site. You create (or use) a plugin, add a command class that extends WP_CLI_Command, define your logic, and register it with WP_CLI::add_command. Then you can run your own commands like wp transients clear or wp cpt list. This speeds up repetitive work and enables scripting and batch processing. Two working examples are below.
The world’s most popular content management system, WordPress, powers millions of websites. Even though a lot of people find its design to be easy to use, developers frequently look for more potent tools to make tedious work easier.
With this blog post, we explore the realm of crafting personalized WP CLI commands, offering WordPress an unprecedented degree of adaptability.
Understanding WP-CLI Commands
WordPress Command Line Interface, or WP-CLI, is a strong and adaptable utility that significantly expands WordPress’s capabilities beyond the conventional admin dashboard.
When compared to typical graphical interfaces, this command line interface facilitates faster and more effective workflow by enabling developers to interact with WordPress sites straight from the command line.
As an illustration, rather than enabling a plugin via the admin dashboard after completing the standard procedure (Dashboard > Plugins > Add New > search for the plugin > Install > Activate), you can use the command wp plugin install <plugin-name> --activate to install and activate a plugin in one step.
Although WP-CLI already includes a core set of commands, the real power lies in a developer realizing the potential of the tool and crafting custom commands tailored to the unique requirements of their website.
Pros and Cons of WP-CLI
WP-CLI is an essential part of the WordPress ecosystem, but like any tool, it comes with trade-offs worth knowing upfront.
|
Pros
|
Cons
|
Creating Custom WP-CLI Commands: An In-Depth Look
There are over 60,000 plugins available for WordPress. The most popular include Yoast SEO, used on over 12 million websites, and Elementor, with over 12 million active installations.
Writing custom WP-CLI commands may sound difficult, but with the right approach, it can be a worthwhile endeavor. Here’s the process, step by step:
|
1
|
Set up a WordPress pluginCreate a new WordPress plugin or put your custom commands within an already-existing plugin. This keeps the project orderly and transparent. |
|
2
|
Create a commands folderMake a ‘commands’ folder inside the plugin directory. Your customized command files will be stored here. |
|
3
|
Structure the command classCreate a PHP file with a class that extends |
Example 1: Clearing All Transients
Transients are a way to temporarily store cached data in WordPress. Sometimes you need to clear all transients to refresh your site’s data. Here’s a custom WP-CLI command for that.
Step 1: Create a plugin
In your wp-content/plugins directory, create a folder called elsner-wp-cli-clear-transients, and inside it, a file named elsner-wp-cli-clear-transients.php
<?php
/*
Plugin Name: Elsner WP-CLI Clear Transients
Description: A simple plugin to clear all transients via WP-CLI.
Version: 1.0
Author: Elsner Technologies
*/
if (defined('WP_CLI') && WP_CLI) {
class Clear_Transients_Command extends WP_CLI_Command {
/**
* Clear all transients.
* @when after_wp_load
*/
public function clear() {
global $wpdb;
$wpdb->query("DELETE FROM $wpdb->options WHERE option_name LIKE '_transient_%'");
$wpdb->query("DELETE FROM $wpdb->options WHERE option_name LIKE '_site_transient_%'");
WP_CLI::success("All transients cleared.");
}
}
WP_CLI::add_command('transients', 'Clear_Transients_Command');
}
Step 2: Activate the plugin
Activate it from the WordPress admin dashboard or via WP-CLI: wp plugin activate elsner-wp-cli-clear-transients
Step 3: Use the command
Run wp transients clear to clear all transients stored in your WordPress database.
Example 2: Listing Custom Post Types
Suppose you want to list all custom post types registered on your WordPress site. Here’s a WP-CLI command for that.
Step 1: Create a plugin
In your wp-content/plugins directory, create a folder called elsner-wp-cli-list-cpt and inside it, a file named elsner-wp-cli-list-cpt.php
<?php
/*
Plugin Name: Elsner WP-CLI List Custom Post Types
Description: A simple plugin to list all custom post types via WP-CLI.
Version: 1.0
Author: Elsner Technologies
*/
if (defined('WP_CLI') && WP_CLI) {
class List_CPT_Command extends WP_CLI_Command {
/**
* List all custom post types.
* @when after_wp_load
*/
public function list() {
$post_types = get_post_types(['_builtin' => false], 'names');
if (empty($post_types)) {
WP_CLI::success("No custom post types found.");
return;
}
foreach ($post_types as $post_type) {
WP_CLI::line($post_type);
}
}
}
WP_CLI::add_command('cpt', 'List_CPT_Command');
}
Step 2: Activate the plugin
Activate it from the WordPress admin dashboard or via WP-CLI: wp plugin activate elsner-wp-cli-list-cpt
Step 3: Use the command
Run wp cpt list to list all custom post types registered on your WordPress site.
Frequently Asked Questions
What is WP-CLI?
WP-CLI is the command line interface for WordPress. It lets you manage a site (plugins, themes, users, content, database) from the terminal instead of the admin dashboard.
How do I create a custom WP-CLI command?
Create or use a plugin, add a class that extends WP_CLI_Command with your method, and register it with WP_CLI::add_command. Then run it from the terminal.
Do I need coding skills to use WP-CLI?
Basic core commands are straightforward, but writing custom commands requires PHP and familiarity with WordPress internals.
Is WP-CLI safe to use on a live site?
Commands run immediately with no undo, so test on staging first and back up before running anything that changes data.
Conclusion
Creating custom WP-CLI commands can significantly enhance your WordPress development workflow, offering speed, efficiency, and flexibility. By following the steps and examples above, you can start crafting your own custom commands tailored to your specific needs, unlocking the full potential of WP-CLI and WordPress.
Need help with a custom WordPress build beyond WP-CLI? Talk to Elsner Technologies’ WordPress development team for a clear read on scope, timeline, and cost.
About Author
Harshal Shah - Founder & CEO of Elsner Technologies
Harshal is an accomplished leader with a vision for shaping the future of technology. His passion for innovation and commitment to delivering cutting-edge solutions has driven him to spearhead successful ventures. With a strong focus on growth and customer-centric strategies, Harshal continues to inspire and lead teams to achieve remarkable results.