@TeamWPGenius
Crafting Custom
WP-CLI Commands:
Unlocking WordPress
Flexibility
By Makarand Mane
v
Founder & designated Partner
WPGenius Solutions LLP
Makarand G. Mane
@mkrndmane
WPGenius.in
v
What we will do in next 30 min.
@mkrndmane wpgenius.in
1. Intro
2. Quick look WP-CLI Class
3. Printing an output
4. Passing arguments to Command
5. Command line help
6. Quick Demo
7. Q&A
Quick Look
WP : WordPress
C : Command
L : Line
I : Interface
v
WP-CLI Quick look
@mkrndmane wpgenius.in
A set of command line tools that allows you to manage WordPress from
the command line.
You need to have SSH access to use WP-CLI.
https://wp-cli.org
v
Who is this for
@mkrndmane wpgenius.in
Anyone really, but mostly:
• Developers
• Theme Designers
• Server Administrators
v
WP-CLI Command Structure
@TeamWPGenius wpgenius.in
wp <command> <subcommand> args1 --flag
E.g.
wp plugin install contact-form-7 --activate
v
Common Use Cases of WP CLI
@mkrndmane wpgenius.in
1. Automated Plugin & Theme Management
2. Database Maintenance and Optimization
3. User Account Administration
4. Content Import and Export
5. Security and Backup Operations
6. Multisite Network Management
7. Media File Handling
8. Post and Page Creation (Creating dummy posts)
9. Child Theme Creation
v
Common issues can be solved with WP-CLI
@TeamWPGenius wpgenius.in
1. Getting time out (502 gateway timeout)
2. Importing lot of data
3. Exporting lot of data with custom formats
4. Write your own command
5. & more…
v
1. Quick look
WP-CLI Class
@mkrndmane wpgenius.in
v
Quick look : WP-CLI Class
@mkrndmane wpgenius.in
WP_CLI::add_command( 'example', 'Example_Command' );
v
Right way to use WP_CLI Class
@mkrndmane wpgenius.in
<?php
/*
* Include only when the WP-CLI is being used
* /
if ( defined( 'WP_CLI' ) && WP_CLI ) {
include_once __DIR__ . '/inc/class-mycommand-cli.php';
}
v
Writing an Example Command
@mkrndmane wpgenius.in
wp example
Output :
Success: Hello World
v
Writing an Example Command
@mkrndmane wpgenius.in
<?php
class Example_Command {
public function __invoke( $args ) {
WP_CLI::success( “Hello WPGenius” );
}
}
WP_CLI::add_command( 'example', 'Example_Command' );
v
Writing an Example Command
With subcommand
@mkrndmane wpgenius.in
wp example hello
Output :
Success: Hello WPGenius
v
@mkrndmane wpgenius.in
<?php
class Example_Command {
public function hello( $args ) {
WP_CLI::success( "Hello WPGenius " );
}
}
WP_CLI::add_command( 'example', 'Example_Command' );
Writing an Example Command
With subcommand
v
2. Printing an
output
@mkrndmane wpgenius.in
v
Printing an output
@mkrndmane wpgenius.in
1. Success -> WP_CLI::success()
2. Error -> WP_CLI::error()
3. Log -> WP_CLI::log()
More you can read in documentation
v
3. Passing args to
Command
@mkrndmane wpgenius.in
v
Passing arguments to Command
@mkrndmane wpgenius.in
WP-CLI command passes 2 type of arguments to the callable handler,
1. Positional argument or an iterator of the unnamed
$args
2. Associative arguments or an iterator of named (sometimes referred to
as “flags”.)
$assoc_args
v
Passing positional argument
@mkrndmane wpgenius.in
wp example hello WPGenius
Output :
Success: Hello WPGenius
v
Passing positional argument
@TeamWPGenius wpgenius.in
class Example_Command {
function hello( $args ) {
$name = $args[0];
WP_CLI::success( "Hello, $name!" );
}
}
v
Passing Associative argument
@mkrndmane wpgenius.in
wp example hello WPGenius --type=error
Output :
Error: Hello WPGenius
v
Passing Associative argument
@mkrndmane wpgenius.in
class Example_Command {
function hello( $args, $assoc_args ) {
$name = $args[0];
$type = $assoc_args['type'];
if( $type == 'error')
WP_CLI::error( "Hello, $name!" );
else
WP_CLI::success( "Hello, $name!" );
}
}
v
4. Command line
help document
@mkrndmane wpgenius.in
wpgenius.in
/**
* 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
*/
v
The shortdesc : What Command does
wpgenius.in
/**
* Prints a greeting.
@mkrndmane
The longdesc : Options & example
* ## 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
v
@mkrndmane
Docblock tags :
wpgenius.in
* @when after_wp_load
*/
@subcommand list
@alias hi
v
https://make.wordpress.org
/cli/handbook/guides/com
mands-cookbook/
@TeamWPGenius wpgenius.in
v
https://github.com/wpgeni
us/WP-Setup-Automate
@TeamWPGenius wpgenius.in
Thank you
Lets Have
some
questions
Get In Touch
Makarand Mane
wpgenius.in
makarand@wpgenius.in
TeamWPGenius
WPGenius

Crafting Custom WP-CLI Commands: Unlocking WordPress Flexibility