As we have seen previously, WP-CLI offers a great way of interacting with WordPress through the command line. However, the true potential of WP-CLI is unlocked when we create custom commands for our specific needs. This blog explores how to create and utilize custom WP-CLI commands to streamline our workflow.
Setting Up Our Custom Commands
Before we can create custom commands, we need to make sure that the WordPress environment is set up fine. For WP-CLI to work we have define the WP_CLI
constant and ensure its available.
if (defined('WP_CLI') && WP_CLI) { class Custom { // Our custom commands will go here } WP_CLI::add_command('example', 'Custom'); }
Defining Custom Commands
Within our Custom
class we can define multiple commands. Each command is a method within the class.
We can start with a simple command that greets a user.
class Custom { /** * Prints a greeting. * * ## OPTIONS * * <name> * : The name of the person to greet. * * [--type=<type>] * : Whether or not to greet the person with success or error. * --- * default: success * options: * - success * - error * --- * * ## EXAMPLES * * wp example hello Newman * * @when after_wp_load */ function hello($args, $assoc_args) { list($name) = $args; // Print the message with type $type = $assoc_args['type']; WP_CLI::$type("Hello, $name!"); } }
In this example, we have created a method called hello
.
This command greets the user. It accepts a name as an argument and an optional --type
argument to determine if the message should be displayed as a success or error.
Fetching Blog Posts
We can add another command to fetch blog posts. This command fetches the latest blog posts using WordPress’s REST API. It retrieves posts and formats them into a table to display the title and link.
class Custom { // ... function blog($args, $assoc_args) { $response = wp_remote_get(rest_url('/wp/v2/posts')); $posts = json_decode(wp_remote_retrieve_body($response)); $output = array(); foreach ($posts as $post) { $output[] = array( 'title' => $post->title->rendered, 'link' => $post->link ); } WP_CLI\utils\format_items('table', $output, array('title', 'link')); } }
Exporting Data
This command exports custom post types as a formatted output. It queries for all my_event
post types retrieves relevant details like title, content, and link and displays them in a specified format.
class Custom { // ... function export_event($args, $assoc_args) { $args = array( 'post_type' => 'my_event', 'posts_per_page' => -1, ); $query = new WP_Query($args); if ($query->have_posts()) { $posts = array(); while ($query->have_posts()) { $query->the_post(); $posts[] = array( 'id' => get_the_ID(), 'title' => get_the_title(), 'content' => get_the_content(), 'link' => get_the_permalink(), ); } WP_CLI\utils\format_items($assoc_args['format'], $posts, array('id', 'title', 'content', 'link')); } } }
Conclusion
Custom WP-CLI commands offer a powerful way to extend the functionality of WP-CLI. By creating custom commands we can streamline our workflow, automate repetitive tasks, and enhance our productivity. Whether it’s greeting users, fetching blog posts, or exporting custom post types, the possibilities with custom commands are endless.
Leave a Reply