>
> wp-cli in-depth > sanjay@astiostech.com
The following slides are provided for informational purposes only. I am not responsible for any actions performed on WordPres s server(s) as a result of using these slides. Essentially please refer to the actual guides,
websites and other official docs. This is not intended, or nor should it be considered as official documentation. Some images, text, information etc. may be copyright materials, and they are reserved by their respective
entities. Distribution and copy is not permitted without written authorization to the authors. All rights reserved.
>> What we’ll cover..
• Why wp-cli
• cli conventions
• Intro to wp-cli
• Requirements
• wp-cli
• Normal usage
• Advanced usage
• Remote usage
• Common issues
• Summary
>>Playing fields
“Command line”
“WordPress”
Beginner Expert
Beginner Expert
>> Our setup
• Debian Linux
• LXC Container
• Up to date OS/Apps
• I use VPN to connect to
the cloud
• With 1GB Ram/8GB HDD 2
CPU cores
>> But why CLI?
• More control & precision
• Make repeated tasks easier
• Powerful - limited by your imagination
• Can do more than GUI/WUI (in WordPress’s case)
• Your first step into coding, perhaps?
• Write ./scripts.php or scripts.sh to automate
• Remote operations
• You can copy/paste
• Join the cool kids 8-)
>> Weapon of choice
WUI(1-5)
CLI(1-
30)
Commercial
and Ci/Cd
tools
• wp-cli
• Best used for single or
multiple sites of around
30 installs that you
manage
• Of course it manage as
many host as you have
>> CLI conventions
• CLI examples
#dir
#ls
• With arguments
#dir /w /O:D
#ls –l –-sort=time
• With positional arguments
#./myscript.php arg1 arg2 arg3
>> Intro to wp-cli
• Written in PHP
• Effort to provide complete alternative to WordPress
• According to GitHub, the first versions were in 2011!
• Current version is 2.3.0, released on Aug 14 this year
• Has over 40 arguments (and their options) which make
them thousands in all
• Hundreds of 3rd party plugin support and growing, e.g
BuddyPress, WP Super Cache
• You can write your own
• Multi-site support (single hardware, many sites)
• Remote operations support (with SSH)
>> Basic requirements
• UNIX-like environment (OS X, Linux, FreeBSD,
Cygwin); limited support in Windows environment
• PHP 5.4 or later
• WordPress 3.7 or later. Versions older than the
latest WordPress release may have degraded
functionality
• Have rights to the WordPress installation
folder, e.g. /var/www/wordpress
• Need CLI access (duh..)
>> wp-cli: Installing
• Get wp-cli. Recommended way is using phar file
#curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-
pages/phar/wp-cli.phar
• Rename wp-cli.phar to wp (easy to use)
#mkdir /home/www-data && chown www-data -R /home/www-data
#cp wp-cli.phar /usr/local/bin/wp
• Make it executable
#chmod +x /usr/local/bin/wp
• Use a non-root user, e.g. www-data
#usermod www-data -s /bin/bash -d /home/www-data
#su www-data
• Test wp cli a’bit
#cd /home/www-data
#wp --info
>> wp-cli: Basic stuff
• RTFM
#wp help
#wp help plugin
• Update your wp-cli
#wp cli check-update
#wp cli update (run as root!)
• Check version of WP
#wp core version --path=/var/www/wordpress
• Check for core updates
#wp core check-update --path=/var/www/wordpress
• Check for plugin updates
#wp plugin update --dry-run --all --path=/var/www/wordpress
>>Using the
wp-cli.yml file
• The config file has
global options, such
as defining the path
of your wp install.
• Three different files
• config.yml
• wp-cli.yml
• wp-cli.local.yml
• Let’s create a wp-
cli.local.yml file for
our ease and add
colors to our wp cli
commands
#nano wp-cli.local.yml
path: /var/www/wordpress
color: true
>> wp-cli: Installing WordPress
• Download, configure DB and install
WordPress
#wp core download (--local=cn)
#wp core config --dbhost=localhost --dbname=wordpress --
dbuser=root --dbpass=mypass --path=/var/www/wordpress (-–
extra-php..)
#wp db create
#wp core install --url=http://url.com --title=CLI --
admin_user=supervisor --admin_password=wpcli123 --
admin_email=sanjay@astiostech.com --path=/var/www/wordpres
>> wp-cli: Inventory stuff
• #wp option --list
• #wp option get blogdescription
• #wp option update blogdescription "Here's to the Joker"
• #wp config list
• #wp checksum core
• #wp checksum plugin akismet
• #wp plugin list --status=inactive [active]
• #wp theme list --status=inactive [active]
• #wp post list (get [1..n])
• Let’s be lazy, make an alias (e.g.)
#alias wp-inactive='wp plugin list --status=inactive && wp theme list --
status=inactive'
>> wp-cli: Installing stuff
• Plugins
#wp plugin install jetpack (--activate)
#wp plugin activate jetpack
• Themes
#wp theme install freedom (--activate)
#wp theme activate freedom
• But I don’t know the plugin/theme name?
#wp plugin/theme search <keyword>
>> wp-cli: Updating stuff
• Updating one plugin or theme
#wp plugin update akismet
#wp theme update twentyfifteen
• Updating all plugin or theme
#wp plugin update --all
#wp theme update --all
• Updating WordPress core itself to specific
version or latest
#wp core version
#wp core check-update
#wp core update --version=4.9.10
#wp core update --minor
#wp core update
#wp core update-db
>> wp-cli: Operational stuff
• Create a post(s)
#wp post create --post_content="Just a cli test" --post_title="Written In CLI" --
post_excerpts="Nothing but a cli test" --post_status=publish
#for i in {1..10}; do wp post create --post_content="Just a cli test no - $i" --
post_title="Written In CLI $i" --post_excerpts="Nothing but a cli test $i" --
post_status=publish; done
• Update a post, enter a tag for a post
#wp post edit 1
#wp post term list 1 post_tag
#wp post update 1 --tags_input='Movies, FunStuff'
• Modifying sidebars
#wp widget list freedom_right_sidebar
#wp widget add categories freedom_right_sidebar --title=Cats
• Delete inactive themes and plugins(Script)
#wp theme delete $(wp theme list --status=inactive --field=name)
• Create users
#wp user create sanjay sanjayws@gmail.com --role=contributor
>> wp-cli: Maintenance stuff
• Enter maintenance mode
#wp maintenance-mode activate [deactive]
• Search and replace
#wp search-replace "CLI" "Command Line Interface" wp_posts --dry-run
#wp search-replace "CLI" "Command Line Interface" wp_posts
• Backing up
#wp export
• Restoring (with a plugin)
#wp plugin install wordpress-importer && wp plugin activate
wordpress-importer
#wp import /home/www-data/wordcampsite1.wordpress.2019-10-31.000.xml
--authors=create
>> wp-cli: Maintenance stuff
• Check if your DB is good, and run optimization
#wp db check
#wp db optimize
• Reset passwords
#wp user update admin --user_pass=wp.123lah
• Performance profiling
#wp package install wp-cli/profile-command
#wp profile hook plugins_loaded --spotlight
>> wp-cli: Advance stuff
• Create and start a webserver using php serve. We
will prepare a new ”site”. Run as root!
#mkdir /home/www-data/wpress2 && cd /home/www-data/wpress2
#wp core download --path=.
#wp core config --dbhost=localhost --dbname=test_wp2 --dbuser=root --
dbpass="" --path=. --allow-root
• Setup a database for WordPress called “test_wp2”
#mysql -e "create database test_wp2;" ( -u –p)
• Setup WordPress basic config
#wp core install --url=http://url.com --title=CLI --admin_user=admin --
admin_password=admin@.123 --admin_email=sanjay@astiostech.com --
path=/home/www-data/wpress2 --allow-root --path=.
• Start temp webserver
#wp server --host=0.0.0.0 --port=1111 --docroot=/home/www-data/wpress2 --
allow-root
>> wp-cli: Remote stuff
• All command that we’ve done previously, can be
executed on a remote server
• There are several ways to do this
• From wp cli
• Using remote tools like plink, the “expect” command
from Linux etc.
• Requires wp cli installed remotely
# wp core version --ssh=www-data@10.88.0.111:/var/www/wordpress
>> wp-cli: Remote stuff
• Passwordless with SSH Keys (go back to root)
• Generate key on local machine
# /usr/bin/ssh-genkey
• Copy that key to remote machine and paste inside
# /usr/bin/ssh-copy-id www-data@10.88.0.111
• Create an alias for easy access, add to .yml file
@prod:
ssh: www-data@10.88.0.111
path: /var/www/wordpress
• Now, let’s update everything on remote using script
#!/bin/bash
myhost=$1
if [[ "$myhost" == "" ]]; then
echo "No host defined"
exit 1
else
wp core version --ssh=www-data@$myhost
wp plugin update --all --ssh=www-data@$myhost
wp theme update --all --ssh=www-data@$myhost
wp core update --ssh=www-data@$myhost
wp core update-db --ssh=www-data@$myhost
wp core version --ssh=www-data@$myhost
exit 0
fi
>> Common issues
• Multisite?
• Define sites in .yml files by its url http://<>
• Running as root
• Don’t
• Add --allow-root
• Can’t find install directory
• Define in wp-cli.yml or wp-cli.local.yml
• Define with flag --path=/..
• Something didn’t work? Debug
• Use --debug
>> Summary
• wp-cli is perfect for mundane tasks
• wp-cli aims to be a complete alternative to the
WUI
• wp-cli is ever growing
• wp-cli can install WordPress, update stuff, manage
posts, fix problems, reset passwords, etc..
• wp-cli is extremely light-weight
• wp-cli has APIs and you can write your own
commands
• wp-cli is totally cool and you must try!
>> References
• wp-cli official:
https://wp-cli.org/
• Installation:
https://make.wordpress.org/cli/handbook/installing/
• Commands reference:
https://developer.wordpress.org/cli/commands/
• Config files:
https://make.wordpress.org/cli/handbook/config/
• Running remotely
https://make.wordpress.org/cli/handbook/running-commands-remotely/
• Profiling
https://guides.wp-bullet.com/using-wp-cli-wp-profile-to-diagnose-
wordpress-performance-issues/
• All Option
https://codex.wordpress.org/Option_Reference
• API guide:
https://make.wordpress.org/cli/handbook/internal-api/
• Common issues:
https://make.wordpress.org/cli/handbook/common-issues/

WordPress CLI in-depth

  • 1.
    > > wp-cli in-depth> sanjay@astiostech.com The following slides are provided for informational purposes only. I am not responsible for any actions performed on WordPres s server(s) as a result of using these slides. Essentially please refer to the actual guides, websites and other official docs. This is not intended, or nor should it be considered as official documentation. Some images, text, information etc. may be copyright materials, and they are reserved by their respective entities. Distribution and copy is not permitted without written authorization to the authors. All rights reserved.
  • 2.
    >> What we’llcover.. • Why wp-cli • cli conventions • Intro to wp-cli • Requirements • wp-cli • Normal usage • Advanced usage • Remote usage • Common issues • Summary
  • 3.
  • 4.
    >> Our setup •Debian Linux • LXC Container • Up to date OS/Apps • I use VPN to connect to the cloud • With 1GB Ram/8GB HDD 2 CPU cores
  • 5.
    >> But whyCLI? • More control & precision • Make repeated tasks easier • Powerful - limited by your imagination • Can do more than GUI/WUI (in WordPress’s case) • Your first step into coding, perhaps? • Write ./scripts.php or scripts.sh to automate • Remote operations • You can copy/paste • Join the cool kids 8-)
  • 6.
    >> Weapon ofchoice WUI(1-5) CLI(1- 30) Commercial and Ci/Cd tools • wp-cli • Best used for single or multiple sites of around 30 installs that you manage • Of course it manage as many host as you have
  • 7.
    >> CLI conventions •CLI examples #dir #ls • With arguments #dir /w /O:D #ls –l –-sort=time • With positional arguments #./myscript.php arg1 arg2 arg3
  • 8.
    >> Intro towp-cli • Written in PHP • Effort to provide complete alternative to WordPress • According to GitHub, the first versions were in 2011! • Current version is 2.3.0, released on Aug 14 this year • Has over 40 arguments (and their options) which make them thousands in all • Hundreds of 3rd party plugin support and growing, e.g BuddyPress, WP Super Cache • You can write your own • Multi-site support (single hardware, many sites) • Remote operations support (with SSH)
  • 9.
    >> Basic requirements •UNIX-like environment (OS X, Linux, FreeBSD, Cygwin); limited support in Windows environment • PHP 5.4 or later • WordPress 3.7 or later. Versions older than the latest WordPress release may have degraded functionality • Have rights to the WordPress installation folder, e.g. /var/www/wordpress • Need CLI access (duh..)
  • 10.
    >> wp-cli: Installing •Get wp-cli. Recommended way is using phar file #curl -O https://raw.githubusercontent.com/wp-cli/builds/gh- pages/phar/wp-cli.phar • Rename wp-cli.phar to wp (easy to use) #mkdir /home/www-data && chown www-data -R /home/www-data #cp wp-cli.phar /usr/local/bin/wp • Make it executable #chmod +x /usr/local/bin/wp • Use a non-root user, e.g. www-data #usermod www-data -s /bin/bash -d /home/www-data #su www-data • Test wp cli a’bit #cd /home/www-data #wp --info
  • 11.
    >> wp-cli: Basicstuff • RTFM #wp help #wp help plugin • Update your wp-cli #wp cli check-update #wp cli update (run as root!) • Check version of WP #wp core version --path=/var/www/wordpress • Check for core updates #wp core check-update --path=/var/www/wordpress • Check for plugin updates #wp plugin update --dry-run --all --path=/var/www/wordpress
  • 12.
    >>Using the wp-cli.yml file •The config file has global options, such as defining the path of your wp install. • Three different files • config.yml • wp-cli.yml • wp-cli.local.yml • Let’s create a wp- cli.local.yml file for our ease and add colors to our wp cli commands #nano wp-cli.local.yml path: /var/www/wordpress color: true
  • 13.
    >> wp-cli: InstallingWordPress • Download, configure DB and install WordPress #wp core download (--local=cn) #wp core config --dbhost=localhost --dbname=wordpress -- dbuser=root --dbpass=mypass --path=/var/www/wordpress (-– extra-php..) #wp db create #wp core install --url=http://url.com --title=CLI -- admin_user=supervisor --admin_password=wpcli123 -- admin_email=sanjay@astiostech.com --path=/var/www/wordpres
  • 14.
    >> wp-cli: Inventorystuff • #wp option --list • #wp option get blogdescription • #wp option update blogdescription "Here's to the Joker" • #wp config list • #wp checksum core • #wp checksum plugin akismet • #wp plugin list --status=inactive [active] • #wp theme list --status=inactive [active] • #wp post list (get [1..n]) • Let’s be lazy, make an alias (e.g.) #alias wp-inactive='wp plugin list --status=inactive && wp theme list -- status=inactive'
  • 15.
    >> wp-cli: Installingstuff • Plugins #wp plugin install jetpack (--activate) #wp plugin activate jetpack • Themes #wp theme install freedom (--activate) #wp theme activate freedom • But I don’t know the plugin/theme name? #wp plugin/theme search <keyword>
  • 16.
    >> wp-cli: Updatingstuff • Updating one plugin or theme #wp plugin update akismet #wp theme update twentyfifteen • Updating all plugin or theme #wp plugin update --all #wp theme update --all • Updating WordPress core itself to specific version or latest #wp core version #wp core check-update #wp core update --version=4.9.10 #wp core update --minor #wp core update #wp core update-db
  • 17.
    >> wp-cli: Operationalstuff • Create a post(s) #wp post create --post_content="Just a cli test" --post_title="Written In CLI" -- post_excerpts="Nothing but a cli test" --post_status=publish #for i in {1..10}; do wp post create --post_content="Just a cli test no - $i" -- post_title="Written In CLI $i" --post_excerpts="Nothing but a cli test $i" -- post_status=publish; done • Update a post, enter a tag for a post #wp post edit 1 #wp post term list 1 post_tag #wp post update 1 --tags_input='Movies, FunStuff' • Modifying sidebars #wp widget list freedom_right_sidebar #wp widget add categories freedom_right_sidebar --title=Cats • Delete inactive themes and plugins(Script) #wp theme delete $(wp theme list --status=inactive --field=name) • Create users #wp user create sanjay sanjayws@gmail.com --role=contributor
  • 18.
    >> wp-cli: Maintenancestuff • Enter maintenance mode #wp maintenance-mode activate [deactive] • Search and replace #wp search-replace "CLI" "Command Line Interface" wp_posts --dry-run #wp search-replace "CLI" "Command Line Interface" wp_posts • Backing up #wp export • Restoring (with a plugin) #wp plugin install wordpress-importer && wp plugin activate wordpress-importer #wp import /home/www-data/wordcampsite1.wordpress.2019-10-31.000.xml --authors=create
  • 19.
    >> wp-cli: Maintenancestuff • Check if your DB is good, and run optimization #wp db check #wp db optimize • Reset passwords #wp user update admin --user_pass=wp.123lah • Performance profiling #wp package install wp-cli/profile-command #wp profile hook plugins_loaded --spotlight
  • 20.
    >> wp-cli: Advancestuff • Create and start a webserver using php serve. We will prepare a new ”site”. Run as root! #mkdir /home/www-data/wpress2 && cd /home/www-data/wpress2 #wp core download --path=. #wp core config --dbhost=localhost --dbname=test_wp2 --dbuser=root -- dbpass="" --path=. --allow-root • Setup a database for WordPress called “test_wp2” #mysql -e "create database test_wp2;" ( -u –p) • Setup WordPress basic config #wp core install --url=http://url.com --title=CLI --admin_user=admin -- admin_password=admin@.123 --admin_email=sanjay@astiostech.com -- path=/home/www-data/wpress2 --allow-root --path=. • Start temp webserver #wp server --host=0.0.0.0 --port=1111 --docroot=/home/www-data/wpress2 -- allow-root
  • 21.
    >> wp-cli: Remotestuff • All command that we’ve done previously, can be executed on a remote server • There are several ways to do this • From wp cli • Using remote tools like plink, the “expect” command from Linux etc. • Requires wp cli installed remotely # wp core version --ssh=www-data@10.88.0.111:/var/www/wordpress
  • 22.
    >> wp-cli: Remotestuff • Passwordless with SSH Keys (go back to root) • Generate key on local machine # /usr/bin/ssh-genkey • Copy that key to remote machine and paste inside # /usr/bin/ssh-copy-id www-data@10.88.0.111 • Create an alias for easy access, add to .yml file @prod: ssh: www-data@10.88.0.111 path: /var/www/wordpress • Now, let’s update everything on remote using script #!/bin/bash myhost=$1 if [[ "$myhost" == "" ]]; then echo "No host defined" exit 1 else wp core version --ssh=www-data@$myhost wp plugin update --all --ssh=www-data@$myhost wp theme update --all --ssh=www-data@$myhost wp core update --ssh=www-data@$myhost wp core update-db --ssh=www-data@$myhost wp core version --ssh=www-data@$myhost exit 0 fi
  • 23.
    >> Common issues •Multisite? • Define sites in .yml files by its url http://<> • Running as root • Don’t • Add --allow-root • Can’t find install directory • Define in wp-cli.yml or wp-cli.local.yml • Define with flag --path=/.. • Something didn’t work? Debug • Use --debug
  • 24.
    >> Summary • wp-cliis perfect for mundane tasks • wp-cli aims to be a complete alternative to the WUI • wp-cli is ever growing • wp-cli can install WordPress, update stuff, manage posts, fix problems, reset passwords, etc.. • wp-cli is extremely light-weight • wp-cli has APIs and you can write your own commands • wp-cli is totally cool and you must try!
  • 25.
    >> References • wp-cliofficial: https://wp-cli.org/ • Installation: https://make.wordpress.org/cli/handbook/installing/ • Commands reference: https://developer.wordpress.org/cli/commands/ • Config files: https://make.wordpress.org/cli/handbook/config/ • Running remotely https://make.wordpress.org/cli/handbook/running-commands-remotely/ • Profiling https://guides.wp-bullet.com/using-wp-cli-wp-profile-to-diagnose- wordpress-performance-issues/ • All Option https://codex.wordpress.org/Option_Reference • API guide: https://make.wordpress.org/cli/handbook/internal-api/ • Common issues: https://make.wordpress.org/cli/handbook/common-issues/