50 Ways to Use WP-CLI to Improve WorkflowStop clicking and start scripting
Welcome
• Andy Gagnon, Portsmouth, NH
• WordPress developer since 2009.
• Director of Web Engineering at Spark Development, LLC
• @andygagnon
What is WP-CLI?A tool for WordPress that runs on the command line. The goal of the project is to provide a method for every action in the WordPress admin.
Why use WP-CLI?Scripts are faster than using a GUI with a
nearly zero error rate.
RequirementsPHP 5.3.29Unix Shell (bash on Mac OS, cygwin on Windows)WordPress 3.7+Command Line skills
How to Install ItSee wp-cli.org for the latest:
$ curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
Test it$ php wp-cli.phar —info
$ chmod +x wp-cli.phar
Check your path$ $PATH$ sudo mv wp-cli.phar /usr/local/bin/wp
Hosts with WP-CLI pre-installed
Bluehost DreamhostGoDaddyHostGatorSiteGroundWordPress.com VIPWPEngine
http://wp-cli.org/docs/hosting-companies/
Workflow
ssh WP-CLI
Text Editor (Sublime)
Grunt
Git
Browser
Browser Dev Tools
Theme/Plugin Development
Need a WP Install$ wp core download$ wp core config —prompt$ wp db create $ wp core install —prompt
Themeswp theme listwp theme search <query>wp theme get <slug>wp theme activate <slug>wp theme delete <slug>wp theme enable <slug>
Need to Set Some Options
GLOBAL OPTIONSwp option listwp option update default_comment_status closedwp option update comment moderation 1
USER METAwp user meta update Andy show_admin_bar_front falsewp user meta update Andy metaboxhidden_dashboard --format=json '["dashboard_activity","dashboard_quick_press","dashboard_widget","dashboard_primary"]'
Manage Widgetswp sidebar listwp widget list sidebar-1wp widget delete meta-2
Manage Pluginswp plugin search <query>wp plugin install developer --activatewp plugin install https://github.com/wp-sync-db/wp-sync-db/archive/master.zip —activatewp plugin delete hello akismet
Manage Posts/Pages$ wp post list$ wp post delete $(wp post list --post_type=page --format=ids) --force
$ wp post create --post_type=page --post_title='Home' --post_status=publish —porcelain)
Manage Menus$ wp menu create 'Menu 1’$ wp menu item add-post menu-1 $homeID
$ wp menu location assign 'Menu 1' primary
Manage Front Page$ wp option update show_on_front page$ wp option update page_on_front <id>$ wp option update page_for_posts <id>
Bringing it All TogetherPROBLEM: Default WordPress install is a blog with no plugins.SOLUTION: Use WP-CLI commands in a script to setup a website of pages.
BASH SCRIPT$ setup-wp.sh
Test Data
Theme Unit Test Data
$ wp plugin install wordpress-importer$ wp plugin activate wordpress-importer
$ curl -O https://wpcom-themes.svn.automattic.com/demo/theme-unit-test-data.xml$ wp import theme-unit-test-data.xml —authors=skip
https://codex.wordpress.org/Theme_Unit_Test
Import Images Into Media Library
$ wp media import ~/path/to/folder/*.jpg
Need a Starter Theme$ wp scaffold _s <slug>$ wp theme activate <slug>
Need a Child Theme$ wp scaffold child-theme <slug> —-prompt$ wp theme activate <slug>
Need to Import or Export XML Data
$ wp plugin install wordpress-importer$ wp export$ wp import <file>
Need to Add a Bunch of Users
wp user import-csv <file> --send-email
Regenerate Thumbnails, Custom Images Sizes
$ wp media regenerate $ wp post list —post_type=attachment$ wp media regenerate <id>
$ seq 760 769 | xargs wp media regenerate
Need a Custom Post Type
$wp scaffold post-type <slug> —theme=<theme-slug>
ADD to functions.phpFLUSH REWRITE CACHE$ wp rewrite flush
Migration
Same Server Migration$ wp db export db.sql$ mv db.sql /path/to/newsite$ cp -r /path/to/oldsite/* /path/to/newsite$ cd /path/to/newsite$ rm wp-config.php$ wp core config —-prompt (new dbname) $ wp db create$ wp db import db.sql$ wp search-replace ‘/path/to/oldsite’ ‘/path/to/newsite’ —-dry-run$ wp search-replace ‘/path/to/oldsite’ ‘/path/to/newsite’$ rm db db.sql
Site Migration New Server
$ cd /path/old$ wp db export db.sql$ scp db.sql [email protected]:/path/new$ tar czvf backup.gz *$ scp backup.gz [email protected]:/path/new$ cd /path/new$ tar -xvzf backup.gz$ rm wp-config.php$ wp core config —-prompt (new dbname) $ wp db create$ wp db import db.sql$ wp search-replace ‘/path/to/oldsite’ ‘/path/to/newsite’ —-dry-run$ wp search-replace ‘/path/to/oldsite’ ‘/path/to/newsite’$ rm db db.sql
Maintenance
Full WordPress Update
$ wp core update$ wp core update-db$ wp theme update —-all$ wp plugin update —-all
Delete All Comments$ wp comment list —-field=ID | xargs wp comment delete —-force
Control Cron Events$ wp cron event list
Need to Flush Cache$wp cache flush
Debugging
White Screen of Death$ wp plugin disable —all$ wp theme activate twenty sixteen
Unable to Connect to Database
$ wp core verify-checksums$ wp db climysql> show databases;mysql> show tables;mysql> select * from wp_users;mysql> exit
No Log In Credentials$ wp user list$ wp user create —-prompt
Has Core Been Modified?
$ wp core verify-checksums
Emergency Backup$ wp db export$ tar czvf backup.gz *
Database Issues$ wp db tables$ wp dp climysql>
Are URLs Correct?$ wp option get home$ wp option get siteurl$ wp option list | head
Run Some PHP$ wp eval <code>
Going Further
Tips: Global Parameters
—-prompt—-path=<path>—-url=<url> for multisite—-user=<username>—-require=<code.php>—-debug verbose output
Tips: Parameters—-format=json,csv,table
—-porcelain return data only
| piping data
Add Commands—-require=<path/to/command-file.php>or Add file towp-cli/php/commands
Community Commands
Advance Custom FieldsEasy Digital DownloadsWP Migrate DB Proand more …
See http://wp-cli.org/docs/tools/
Packages10up - Move a WP site to a multisite installation.
$ wp package install <package-name>
See http://wp-cli.org/package-index/
YML Config Files
1) Command line flags2) wp-cli.local.yml3) wp-cli.yml4) ~/.wp-cli/config.yml5) Defaults
http://wp-cli.org/config/
FutureWP REST API
All WP-CLI commands will be available via WP REST API.
WP-CLI Advantages-Use one tool instead of a mix of plugins-Future-proof.-Uses WordPress core.-KISS philosophy.
ThanksDaniel BachhuberShawn Hooper
Learn Morehttp://wp-cli.org
http://wp-cli.org/docs/commands-cookbook/