WP-CLI Commands and Custom Commands

Summary: Learning about WP-CLI and its different commands, Creating Custom WP-CLI commands.

WP-CLI

WP-CLI’s goal is to offer a complete alternative to the WordPress admin for any action we might want to perform in the WordPress admin there should be an equivalent WP-CLI command. A command is an atomic unit of WP-CLI functionality. wp plugin install is one such command as is wp plugin activate. Commands are useful to WordPress users because they can offer simple precise interfaces for performing complex tasks.

WP-CLI Commands Cheatsheet

Core Commands

  • wp core download: Downloads the latest version of WordPress core.
  • wp core config: Generates the wp-config.php file with specified database credentials.
  • wp core install: Installs WordPress with a given site URL, title, admin user, etc.
  • wp core update: Updates the WordPress core to the latest version.

Database Commands

  • wp db create: Creates a new database.
  • wp db drop: Drops the database.
  • wp db export: Exports the database to an SQL file.
  • wp db import: Imports an SQL file into the database.
  • wp db optimize: Optimizes the database tables.
  • wp db repair: Repairs the database tables.

Plugin Commands

  • wp plugin install : Installs a plugin by slug.
  • wp plugin activate : Activates a plugin.
  • wp plugin deactivate : Deactivates a plugin.
  • wp plugin update : Updates a plugin to its latest version.
  • wp plugin delete : Deletes a plugin.

Theme Commands

  • wp theme install : Installs a theme by slug.
  • wp theme activate : Activates a theme.
  • wp theme update : Updates a theme to its latest version.
  • wp theme delete : Deletes a theme.

User Commands

  • wp user create –role=: Creates a new user with the specified role.
  • wp user list: Lists all users on the site.
  • wp user delete : Deletes a user by ID.
  • wp user update –=: Updates user fields (e.g., email, role).

Post Commands

  • wp post create: Creates a new post.
  • wp post delete : Deletes a post by ID.
  • wp post update –=: Updates post fields (e.g., title, content).
  • wp post list: Lists posts based on various parameters (e.g., post type, status).

Option Commands

  • wp option get : Retrieves a specific option.
  • wp option update : Updates the value of a specific option.
  • wp option delete : Deletes a specific option.

Media Commands

  • wp media import : Imports a media file into the media library.
  • wp media regenerate: Regenerates thumbnails for image attachments.

Rewrite Commands

  • wp rewrite list: Lists all rewrite rules.
  • wp rewrite flush: Flushes the rewrite rules.

Custom WP-CLI Commands

Creating custom WP-CLI commands allows us to extend the functionality of WP-CLI to meet the specific needs of our WordPress site.

  • Create a plugin or add the command to our theme’s functions.php.
  • Use the WP_CLI::add_command() function to register our custom command.
<?php
if (defined('WP_CLI') && WP_CLI) {
    WP_CLI::add_command('list_posts', 'List_Posts_Command');
}

class List_Posts_Command {
    public function __invoke($args, $assoc_args) {
        $post_type = $assoc_args['post_type'] ?? 'post';
        $posts = get_posts(['post_type' => $post_type]);

        if (empty($posts)) {
            WP_CLI::success("No posts found.");
            return;
        }

        foreach ($posts as $post) {
            WP_CLI::log("{$post->ID}: {$post->post_title}");
        }
    }
}
  • Usage: wp list_posts --post_type=page

Displaying Output

    • Use WP_CLI::log() to display standard output.
    • Use WP_CLI::success() to indicate successful execution.
    • Use WP_CLI::error() to terminate the command and display an error message.

    Custom WP-CLI Command Documentation

      We can use doc comments to document our custom commands. WP-CLI automatically uses these comments for the wp help command.

      /**
       * List all posts of a specific post type.
       *
       * ## OPTIONS
       *
       * [--post_type=<post_type>]
       * : The post type to list.
       *
       * ## EXAMPLES
       *
       *     wp list_posts --post_type=page
       *
       * @when after_wp_load
       */
      
      «
      »

      Leave a Reply

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