Ryan Kanner (@codeprokid)
Download the .phar file
$ curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
Check if it's working
$ php wp-cli.phar --info
Make it executable, and put it in your PATH
$ chmod +x wp-cli.phar
$ sudo mv wp-cli.phar /usr/local/bin/wp
That's it!
$ wp --info
PHP binary: /usr/bin/php5
PHP version: 5.5.9-1ubuntu4.14
php.ini used: /etc/php5/cli/php.ini
WP-CLI root dir: /home/wp-cli/.wp-cli
WP-CLI packages dir: /home/wp-cli/.wp-cli/packages/
WP-CLI global config: /home/wp-cli/.wp-cli/config.yml
WP-CLI project config:
WP-CLI version: 1.1.0
# Download a fresh copy of WordPress
wp core download
# Update Core
wp core update
# Update a plugin
wp plugin update jetpack
# Install a theme
wp theme install twentyseventeen --activate
# Create a term
wp term create category Rabbits --parent=animals
# Get information on a user
wp user get 10
# Update an option
wp option update my_favorite_pet Rabbits
# Delete post ID 123
wp post delete 123 --force=true
# Regenerate all thumbnails
wp media regenerate
# Delete expired transients
wp transient delete --expired
# Delete all transients
wp transient delete --all
# List of scheduled cron events
wp cron event list
# Run all cron events in queue
wp cron event run --due-now
# Flush rewrite rules
wp rewrite flush
# List of rewrites
wp rewrite list --format=csv
# Flush the cache
wp cache flush
--path=<path>
//Path to the WordPress files.
--ssh=[<user>@]<host>[:<port>][<path>]
//Perform operation against a remote server over SSH.
--http=<http>
//Perform operation against a remote WordPress install over HTTP.
--url=<url>
//Pretend request came from given URL. In multisite, this
//argument is how the target site is specified.
--skip-plugins[=<plugin>]
//Skip loading all or some plugins. Note: mu-plugins are still loaded.
--skip-themes[=<theme>]
//Skip loading all or some themes.
--skip-packages
//Skip loading all installed packages.
--require=<path>
//Load PHP file before running the command (may be used more than once).
--[no-]color
//Whether to colorize the output.
--quiet
//Suppress informational messages.
// Delete all pages
$ wp post delete $(wp post list --post_type='page' --format=ids)
// Delete all posts in the trash
$ wp post delete $(wp post list --post_status=trash --format=ids)
// Update an option for all sites in a network
$ wp site list --field=url | xargs -n1 -I {} sh -c 'wp --url={} option update my_option my_value'
// Import tags from a CSV
#Usage ./import_tags <file-name>
while IFS=, read line
do
eval wp term create post_tag "\""$line"\""
done < $1
# Generate 100 dummy posts
wp post generate --format=ids --count=100 | xargs -n1 -I % wp term meta add % generated_data true
# Generate 10 dummy categories
wp term generate category --format=ids --count=10 | xargs -n1 -I % wp term meta add % generated_data true
# Generate 100 dummy users
wp user generate --format=ids --count=100 | xargs -n1 -I % wp term meta add % generated_data true
# Clean It Up
wp term list category --field=term_id --meta_key=generated_data | xargs wp term delete category
wp user list --field=user_id --meta_key=generated_data | xargs wp user delete
wp post list --format=ids --meta_key=generated_data | xargs wp post delete
# Add this to your ~/.bashrc
syn_prod_db() {
wp search-replace $1 $2 --export=db.sql
wp @production db export backup.sql
wp @production db import db.sql
wp @production db optimize
}
# Usage
sync_prod_db mysite.dev mysite.com
# Global parameter defaults
path: wp-core
url: http://example.com
user: admin
color: false
disabled_commands:
- db drop
- plugin install
require:
- path-to/command.php
# Subcommand defaults (e.g. `wp core config`)
core config:
dbuser: root
dbpass:
extra-php: |
define( 'WP_DEBUG', true );
define( 'WP_POST_REVISIONS', 50 );
// Project level
@staging:
ssh: wpcli@staging.wp-cli.org
user: wpcli
path: /srv/www/staging.wp-cli.org
@production:
ssh: wpcli@wp-cli.org:2222
user: wpcli
path: /srv/www/wp-cli.org
// Global locally
@site1
path: /projects/project_1_root
@site2
path: /projects/project_2_root
@site3
path: /projects/project_3_root
@site4
path: /projects/project_4_root
# Search & Replace Local DB & export to .sql file
wp search-replace mysite.dev mysite.com --export=db.sql
# Take a backup of the old DB
wp @production db export backup.sql
# Import the new DB
wp @production db import db.sql
# Optimize the DB after import
wp @production db optimize
// Class to contain our command
Class My_Command extends WP_CLI {
// Any public method within the class becomes a command
public function say_hello( $args, $assoc_args ) {
$name = ( isset( $args[0] ) ) : $args[0] : 'Anonymous';
WP_CLI::success( 'Hello ' . $name );
}
}
// Registers all commands under the `sample` sub-command
WP_CLI::add_command( 'sample', 'My_Command' );
# How to run this command:
wp sample say_hello Ryan
Success: Hello Ryan
// Class to contain our command
Class My_Command extends WP_CLI {
// Any public method within the class becomes a command
public function say_hello( $args, $assoc_args ) {
$name = ( isset( $args[0] ) ) : $args[0] : 'Anonymous';
$excited = WP_CLI\Utils\get_flag_value( $assoc_args, 'excited', false );
$message = 'Hello ' . $name;
$message .= ( 'true' === $excited ) ? '!' : '';
WP_CLI::success( $message );
}
}
// Registers all commands under the `sample` sub-command
WP_CLI::add_command( 'sample', 'My_Command' );
# Running the following:
wp sample say_hello Ryan --excited=true
# Will return
Success: Hello Ryan!
/**
* Command for saying hello to people
*
* # OPTIONS
*
* <name>
* : Who we should say hello to
*
* [--excited]
* : Whether or not to append an exclamation mark to the message
* ---
* default: false
* options:
* - false
* - true
* ---
*
* @subcommand say-hello
* @when before_wp_load
*/
public function say_hello( $args, $assoc_args ){ /* code */ };
class Term_Mover extends WP_CLI {
public function move_terms( $args, $assoc_args ) {
$delete_old_term = WP_CLI\Utils\get_flag_value( $assoc_args, 'delete-old-term', false );
// Get all the terms from the old taxonomy
$old_terms = get_terms( array( 'taxonomy' => $args[0] ) );
// Make sure there are terms to move
if ( empty( $old_terms ) || is_wp_error( $old_terms ) ) {
WP_CLI::error( 'Could not find any terms to move' );
}
foreach ( $old_terms as $term ) {
$term_id = wp_insert_term( $term->name, $args[1] );
if ( false !== $term_id && ! is_wp_error( $term_id ) ) {
WP_CLI::success( 'Successfully inserted term id: ' . $term_id );
if ( 'true' === $delete_old_term ) {
wp_delete_term( $term->term_id, $args[0] );
}
} else {
WP_CLI::error( 'Could not insert new term' );
}
}
}
}
WP_CLI::add_command( 'sample', 'Term_Mover' );
// Only load our CLI command when loaded via WP_CLI.
if ( defined( 'WP_CLI' ) && WP_CLI ) {
require_once dirname( __FILE__ ) . '/my-cli-class.php';
}
You can actually add a new command to an existing sub-command. See below:
// Class to contain our command
Class My_Command extends WP_CLI {
// Any public method within the class becomes a command
public function say_hello( $args, $assoc_args ) {
$name = ( isset( $args[0] ) ) : $args[0] : 'Anonymous';
WP_CLI::success( 'Hello ' . $name );
}
}
// Registers all commands under the `plugin` sub-command
WP_CLI::add_command( 'plugin', 'My_Command' );
# Usage
wp plugin say_hello Ryan
$assoc_args = wp_parse_args( $assoc_args, array(
'foo' => true,
'bar' => array(),
);
$count = 100;
$progress = \WP_CLI\Utils\make_progress_bar( 'Generating users', $count );
for ( $i = 0; $i < $count; $i++ ) {
// Your code
$progress->tick();
}
$progress->finish();
$count = 100;
$progress = \WP_CLI\Utils\make_progress_bar( 'Generating users', $count );
for ( $i = 0; $i < $count; $i++ ) {
// Your code
$progress->tick();
}
$progress->finish();
// Run an existing CLI command within another command
$plugins = WP_CLI::runcommand( 'plugin list --format=json', array(
'return' => true, // Return `STDOUT` from command
'parse' => 'json', // Convert the `STDOUT` to a json obj
'launch' => false, // re-use same process so we can capture result
'exit_error' => true, // Halt the entire script on error
) );
You can easily save the output of any command that gives you a STDOUT with the > operator
# Save all of the terms in the post_tag taxonomy to a CSV
wp term list post_tag --field=name --format=csv > mytags.csv