WP-CLI is an awesome WordPress plugin that can be used to automate a bunch of difficult WordPress tasks. This presentation walks you through basic WP-CLI commands and creating custom commands.
2. Who Am I?
• My name is Taylor Lovett
Senior Strategic Engineer at 10up
Plugin Author (Safe Redirect Manager, Feed Pull)
Plugin Contributor (WP-CLI and others)
Core Contributor
4. What Is WP-CLI?
• WP-CLI is a WordPress plugin that enables you to
interact with your WordPress installations via the
command line (Unix).
• WP-CLI lets you do a ton of things - create new
sites, install plugins, import, export, batch
create/delete posts, etc.
• WP-CLI is extensible. You can easily write your own
WP-CLI commands.
5. Why Use WP-CLI?
• Doing certain tasks from within WP admin are very
difficult…
- Bulk deleting posts, bulk editing posts, bulk tagging
posts, importing, exporting, etc.
9. Search and replace
wp search-replace <old-string> <new-string> …
(Full list of command options at http://wp-cli.org/commands/search-replace/)
Example:
wp search-replace testsite.com livesite.com wp_posts
10. Generating Dummy Data
• Ever wish you had a bunch of test data when
building a new theme or plugin?
wp generate posts --count=500
wp generate users --role=contributor
11. Regenerate Post
Thumbnails
• Easily regenerate post thumbnails without having to
deal with plugins and WP admin:
wp media regenerate
(From http://wp-cli.org/commands/media/regenerate/)
12. Create Database Dumps
• WP-CLI lets you easily create database dumps:
wp db dump
(From http://wp-cli.org/commands/db/)
15. Global Command
Parameters
--user=set the current user
--url=set the current URL
--path=set the current path to the WP install
--require=load a certain file before running the command
--versionprint WP-CLI version
• These parameters can be supplied to any WP-CLI
command
16. Global Command
Parameters
• The --path argument is especially useful. You can
call the “wp” command from anywhere in the file
system and specify an instance of WordPress to
interact with.
• This is helpful if you are writing a crontab file, for
example.
17. WP-CLI and Multisite
• WP-CLI works great with multisite! The global url
parameter mentioned earlier lets you specify on
which site to run the command. For example
wp --url=http://mysite.com/site2 post delete 3
18. Command and
Subcommands
• WP-CLI has this notion of commands and
subcommands.
wp post delete 476
• “post” is the command. “delete” is the subcommand
19. Community Commands
• There is a large community of people developing
WP-CLI commands:
http://wp-cli.org/package-index/
20. Creating New Commands
• Sometimes we need to create new WP-CLI
commands because the built-in ones don’t do what
we want and an HTTP request just isn’t cutting it.
21. Creating Commands
• You can create a new WP-CLI command in a plugin or a
theme. For a theme, this code can be added to functions.php,
for a plugin, any PHP file that is parsed:
<?php
if ( defined( 'WP_CLI' ) && WP_CLI ) {
include( 'includes/class-prefix-cli-utils.php' );
}
Note: We don’t want to include our CLI class on normal
HTTP requests which is why we check if WP_CLI is
defined.
22. Creating Commands
• Remember how I mentioned commands and sub-
commands? I generally create one command per
plugin or theme and group everything as
subcommands under it.
23. Creating Commands
• Here is includes/class-prefix-cli-utils.php:
<?php
/**
* Utility commands for my theme/plugin
*/
class Prefix_CLI_Utils extends WP_CLI_Command {
...
}
WP_CLI::add_command( 'prefix-utils', 'Prefix_CLI_Utils' );
Note: We prefix classes and everything that is in a shared
namespace so we don’t get any conflicts.
25. A Problem to Solve
• Let’s imagine we are running a WordPress website
with ~15,000 posts. We’ve decided we want to start
making better use of post formats, specifically
gallery. We want every post that has a [gallery]
shortcode to have a post format of gallery. Is there
anyway to do this without going through each post
manually?
27. Creating Our Command
<?php
/**
* Utility commands for my theme/plugin
*/
class Prefix_CLI_Utils extends WP_CLI_Command {
/**
* Mark posts as gallery format if a gallery shortcode is in the post.
*
* ###OPTIONS
*
* [—-post_type=<post_type>]
* : Restrict processing to certain post types
*
* [--status=<status>]
* : Only process posts with this status
*
* @subcommand format-gallery-posts
* @synopsis [—-post_type=<post_type>] [--status=<status>]
*/
public function format_gallery_posts( $args, $assoc_args ) {
}
}
WP_CLI::add_command( 'prefix-utils', 'Prefix_CLI_Utils' );
28. <?php
/**
* Utility commands for my theme/plugin
*/
class Prefix_CLI_Utils extends WP_CLI_Command {
/**
* Mark posts as gallery format if a gallery shortcode is in the post.
*
* ###OPTIONS
*
* [--post_type=<post_type>]
* : Restrict processing to certain post types
*
* [--status=<status>]
* : Only process posts with this status
*
* @subcommand format-gallery-posts
* @synopsis [--post_type=<post_type>] [--status=<status>]
*/
public function format_gallery_posts( $args, $assoc_args ) {
$status = 'publish';
if ( ! empty( $assoc_args['status'] ) ) {
$status = $assoc_args['status'];
}
$post_type = 'post';
if ( ! empty( $assoc_args['post_type'] ) ) {
$post_type = $assoc_args['post_type'];
}
$page = 1;
$posts_changed = 0;
$posts_processed = 0;
WP_CLI::line( 'Start processing posts...' );
while ( true ) {
30. Another Problem
• We’ve been running a blog for a few years with
around ~4000 posts. We decide that we want to
restructure our categories. For the last few years we
have prepended “Breaking News: “ to important
news posts. We want to categorize all these posts
into a “News” category. We don’t want to go through
each post manually.