In this session, we’ll delve into the world of WP-CLI, a powerful command-line tool for WordPress. We’ll start with an introduction to WP-CLI, breaking down its simple command structure, and explore its potential beyond the basics.
The focus of this session is to empower you to craft your own WP-CLI commands, enabling you to tackle specific issues that aren’t addressed by core commands. We’ll explore practical scenarios where custom commands can come to the rescue, such as avoiding common 502 errors or extending plugin functionality with unique commands.
We’ll shed light on the WP-CLI class, providing insights into how to harness its capabilities. The highlight of this session will be a live demonstration of writing your custom WP-CLI command. You’ll walk away with the knowledge and confidence to create commands tailored to your unique needs, empowering your WordPress development journey.
2. v
Founder & designated Partner
WPGenius Solutions LLP
Makarand G. Mane
@mkrndmane
WPGenius.in
3. 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
5. 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
6. v
Who is this for
@mkrndmane wpgenius.in
Anyone really, but mostly:
• Developers
• Theme Designers
• Server Administrators
8. 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
9. 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…
11. v
Quick look : WP-CLI Class
@mkrndmane wpgenius.in
WP_CLI::add_command( 'example', 'Example_Command' );
12. 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';
}
13. v
Writing an Example Command
@mkrndmane wpgenius.in
wp example
Output :
Success: Hello World
14. 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' );
15. v
Writing an Example Command
With subcommand
@mkrndmane wpgenius.in
wp example hello
Output :
Success: Hello WPGenius
16. 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
18. 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
20. 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
26. 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
*/
27. v
The shortdesc : What Command does
wpgenius.in
/**
* Prints a greeting.
@mkrndmane
28. 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