WP-CLI is a command line interface for managing WordPress installations. It allows users to perform tasks like installing plugins and themes, updating WordPress core, managing users, and more through commands instead of using the WordPress dashboard. The tool saves time by automating repetitive tasks and allowing bulk operations. Developers can extend WP-CLI's functionality by creating their own commands and packages.
6. VERY SHORT HISTORY
• Open source Project created in 2011
https://github.com/wp-cli/wp-cli/
• Project maintained by Daniel Bachhuber.
• Initial code by Andreas Creten and Cristi Burcă (scribu).
• Officially supported by WordPress.org since December 2016
12. WHAT IS WP-CLI
WP-CLI is a set of command-line tools for
managing WordPress installations. You can
update plugins, configure multisite installs and
much more, without using a web browser.
14. MINIMUM REQUIREMENTS
• UNIX-like environment (OS X, Linux, FreeBSD, Cygwin);
(limited support in Windows environment)
• PHP 5.3.29 or later (php 5.3 EOL 14 Aug 2014)
• WordPress 3.7 or later (released on October 2013)
• SSH access
• no root privileges needed*
• http://wp-cli.org/
15. HOW TO INSTALL IT (locally)
curl -O https://raw.githubusercontent.com/wp-
cli/builds/gh-pages/phar/wp-cli.phar
php wp-cli.phar --info
or
Download it manually and copy it into document root
NOTICE: if installed locally WP-CLI must be placed in the same folder of WP core
files (i.e. public_html)
https://make.wordpress.org/cli/handbook/installing/
16. HOW TO INSTALL IT (globally)
curl -O https://raw.githubusercontent.com/wp-
cli/builds/gh-pages/phar/wp-cli.phar
php wp-cli.phar --info
chmod +x wp-cli.phar
sudo mv wp-cli.phar /usr/local/bin/wp
wp --info
https://make.wordpress.org/cli/handbook/installing/
18. USE VVV TO TRY IT
• VVV (Variable Varying Vagrant) provides WP-CLI
• VVV uses WP-CLI under the hood to create new WP install
https://www.slideshare.net/AndreaCardinali/professional-wordpress-development-with-vagrant-andrea-cardinali-wordcamp-milano-2016
19. USE WP-CLI POWERED HOSTING
https://make.wordpress.org/cli/handbook/hosting-companies/
22. WP-CLI GLOBAL ARGS
--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.
--user=<id|login|email>Set the WordPress user.
--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-packagesSkip loading all installed packages.
--require=<path>Load PHP file before running the command (may be used more than once).
--[no-]colorWhether to colorize the output.
--debug[=<group>]Show all PHP errors; add verbosity to WP-CLI bootstrap.
--prompt[=<assoc>]Prompt the user to enter values for all command arguments, or a subset
specified as comma-separated values.
--quietSuppress informational messages.
25. EXTENSIBLE
• Extend functionalities with packages
• Write your own functionality (more on this later)
• You can concat multiple commands (pipe)
• SSH support out of the box (control your WP from remote)
29. SAVE TIME
• DRY: Don’t Repeat Yourself
• You don’t have to login / visit url/ wait for page loads
• You can export data in CSV, XML, JSON
• You can pilot multiple installation at once
30. TAKE AWAY
• WP-CLI is a swiss army knife for wp users
• WP-CLI can be installed if not available on your hosting
• WP-CLI let you save a lot of time
31. HOW LONG IT TAKES TO PERFORM THE
FOLLOWING OPERATIONS?
1. Download WordPress
2. Install theme
3. Install and activate plugins
4. Create users
5. Import backup (from dev)
6. Replace production url
42. INSTALL MULTIPLE PLUGINS AT ONCE
cat plugins.txt | xargs wp plugin install
https://gist.github.com/Cardy/347807cf73681783f1fb6c2911a7cddd
43. INSTALL MULTIPLE PLUGINS AT ONCE WITH
SPECIFIC VERSION
sh plugins.sh
https://gist.github.com/Cardy/347807cf73681783f1fb6c2911a7cddd
#!/bin/bash
input="./plugins.txt"
# Set "," as the field separator using $IFS # and read line by line using while read combo
while IFS=',' read -r f1 f2 do
if [ -z "$f1" ] then continue;
fi
echo "Installing $f1 $f2"
if [ -z "$f2" ]
then wp plugin install $f1 --activate >> install.log 2>&1
else wp plugin install $f1 --version=$f2 --force --activate >> install.log 2>&1
fi
done < "$input"
52. USEFUL SUBCOMMANDS
BACKUP / RESTORE DB
wp db [subcommand]
wp db export filename.sql
wp db import filename.sql
53. USEFUL SUBCOMMANDS
WORKS ON DB WITHOUT phpMyAdmin
wp db [subcommand]
wp db create
wp db drop --yes
wp db query < my_query.sql
wp db optimize
http://wp-cli.org/commands/db/
55. USEFUL SUBCOMMANDS
USERS AND CAPABILITIES
wp user [subcommand]
wp user list
wp user create username email --role=rolename
wp user update 1 --user_pass=‘changeMe123’
wp user import-csv
http://wp-cli.org/commands/user/
58. YAML FILE
You can define a wp-cli.yaml to store default configuration
1. inline args
2. wp-cli.local.yml file inside the current working directory (or
upwards).
3. wp-cli.yml file inside the current working directory (or
upwards).
4. ~/.wp-cli/config.yml file WP-CLI defaults.
62. (SOME) USEFUL WP-CLI PACKAGES
wp-cli/restful
binarygary/db-checkpoint
runcommand/assign-featured-images
and many more
https://wp-cli.org/package-index/
63. WP-CLI RESTFUL
RESTful WP-CLI makes WP REST API endpoints available as WP-
CLI commands.
https://github.com/wp-cli/restful
66. WP-CLI EXECUTION
• WP_CLI::launch() - Launch an arbitrary external process that
takes over I/O.
• WP_CLI::launch_self() - Run a WP-CLI command in a new
process reusing the current runtime arguments.
• WP_CLI::runcommand() - Run a WP-CLI command.
• WP_CLI::run_command() - Run a given command within the
current process using the same global
68. WP-CLI HOOKS
• WP_CLI::add_hook() - Schedule a callback to be executed at a
certain point.
• WP_CLI::do_hook() - Execute callbacks registered to a given
hook.
• WP_CLI::add_wp_hook() - Add a callback to a WordPress
action or filter.
• WP_CLI::add_command() - Register a command to WP-CLI.
69. WP-CLI HOOKS
WP_CLI::add_hook( $when, $callback)
esegue $callback quando viene scatenato $when
WP_CLI::do_hook( $when )
scatena $when che può essere agganciato tramite add_hook
70. WP-CLI $when
before_invoke:<command> - Just before a command is invoked.
after_invoke:<command> - Just after a command is involved.
before_wp_load - Just before the WP load process begins.
before_wp_config_load - After wp-config.php has been located.
after_wp_config_load - After wp-config.php has been loaded into scope.
after_wp_load - Just after the WP load process has completed.
71. WP-CLI OUTPUT
• WP_CLI::log() - Display informational message without prefix.
• WP_CLI::success() - Display success message prefixed with "Success: ".
• WP_CLI::debug() - Display debug message prefixed with "Debug: " when `--
debug` is used.
• WP_CLI::warning() - Display warning message prefixed with "Warning: ".
• WP_CLI::error() - Display error message prefixed with "Error: " and exit
script.
• WP_CLI::halt() - Halt script execution with a specific return code.
73. CREATE YOUR OWN PACKAGE
1. Create a new command
2. Extend an existing one (create a subcommand)
3. Make use of PHPDoc
4. Include your package inside a plugin or add it to wp
package index
https://make.wordpress.org/cli/handbook/commands-cookbook/
74. CREATE YOUR OWN PACKAGE
https://make.wordpress.org/cli/handbook/commands-cookbook/ https://wp-cli.org/docs/internal-api/
<?php
class Cardy_Command
{
public function greetings( $args )
{
WP_CLI::success(‘Hello World');
}
}
$instance = new Cardy_Command();
WP_CLI::add_command( 'cardy', $instance );
75. ADD PACKAGE TO YOUR PLUGIN
if ( defined( 'WP_CLI' ) && WP_CLI ) {
require_once dirname( __FILE__ ) . '/inc/class-
plugin-cli-command.php';
}
81. WORDPRESS MEETUP ROMAGNA
QUANDO:
ogni 1° giovedi del mese
DOVE:
Dinamo Coworking Space Cesena
PERCHÉ:
Per parlare di WordPress, conoscere bella gente e condividere
le proprie esperienze
wpromagna.com
@romagnawp