Upgrade to Pro — share decks privately, control downloads, hide ads and more …

WP CLI: An Intro and Interesting Use Cases

WP CLI: An Intro and Interesting Use Cases

WP-CLI is the command line interface for WordPress. In this talk, I introduce the core concepts of WP-CLI, talk about how to use it to manage your websites, and cover a few interesting use cases for it.

If you’ve never heard of WP-CLI, or you’re an experienced user, hopefully there will be something of interest for you in this talk.

John Blackbourn

April 26, 2018
Tweet

More Decks by John Blackbourn

Other Decks in Technology

Transcript

  1. Interfaces to WordPress • Admin area • Ajax • REST

    API • XML-RPC • RSS feeds • Themes • Comments
  2. WP-CLI is a command line interface for WordPress. The project’s

    goal is to offer a complete alternative to the WordPress admin. For any action you might want to perform in the WordPress admin, there should be an equivalent WP-CLI command.
  3. $

  4. Built-in User Manual wp help wp help <command> wp <command>

    --help Guided Interactive Mode wp <command> --prompt How to Get Help
  5. Generally follows "noun [noun]... verb" format wp user create wp

    post meta add wp plugin install But not always... wp import Command Structure
  6. One-off usage in the terminal: How to use $ wp

    user create john [email protected] --role=editor Success: Created user 6. $ wp plugin list +-------------------------+----------+--------+---------+ | name | status | update | version | +-------------------------+----------+--------+---------+ | airplane-mode | active | none | 0.2.4 | | email-log | inactive | none | 2.2.5 | | query-monitor | active | none | 3.0.0 | | revisions-digest | inactive | none | 0.1.0 | | rewrite-rules-inspector | inactive | none | 1.2.1 | | user-switching | active | none | 1.3.1 | | wordpress-importer | active | none | 0.6.4 | | wp-crontrol | inactive | none | 1.7 | | db.php | dropin | none | | | object-cache.php | dropin | none | | +-------------------------+----------+--------+---------+
  7. For scripting purposes: How to use #!/usr/bin/env bash # Configure

    the script to exit immediately if any command fails: set -e # Download WordPress core files if they're not already present: if [ ! -f "wp-load.php" ]; then wp core download else echo "WordPress files are already present." fi # Create a wp-config.php file if it's not already present: if [ ! -f "wp-config.php" ]; then echo "Please enter your database credentials:" wp config create --prompt fi
  8. $ wp post list $ wp post create --post_title="Hello World"

    $ wp post edit $ wp term list category $ wp term list post_tag
  9. $ wp theme list $ wp theme install wpldn --activate

    $ wp theme mod set colour-scheme light $ wp widget move widget-4 --position=2 $ wp menu delete all-pages $ wp theme --edit // nope
  10. $ wp user list $ wp user list --search=john $

    wp user list --role=editor $ wp user create emily [email protected] $ wp user update john --user_password=hunter2
  11. $ wp option get blogname $ wp option update admin_email

    [email protected] $ wp option list --search="thumbnail_*" $ wp rewrite structure '%post_id%'
  12. WP-CLI is Fast 1. Navigate to admin area 2. Enter

    username 3. Enter password 4. Click on Plugins 5. Click Add New 6. Search for plugin name 7. Find plugin in results 8. Click "Install"... 9. Click "Activate"... $ wp plugin install wordpress-seo --activate
  13. WP-CLI is Efficient $ wp user update $(wp user list

    --search="*example.com" --search-fields=email --format=ids) --role=subscriber Success: Updated user 6. Success: Updated user 8. Success: Updated user 9. Success: Updated user 7. Success: Updated user 10. Success: Updated user 15. Success: Updated user 18. Success: Updated user 27. Success: Updated user 34. Success: Updated user 35. Success: Updated user 37. Success: Updated user .....
  14. Prevent users from using the installers/editors/updaters: define( 'DISALLOW_FILE_MODS', true );

    or wp config set DISALLOW_FILE_MODS true --type=constant --raw WP-CLI remains unaffected; allows for controlled management: wp core language install it_IT wp plugin update --all wp theme install twentyeighteen Enhance Your Security
  15. Skip loading all or some plugins or themes: --skip-plugins --skip-plugins=a-slow-plugin

    --skip-themes Load PHP file before running the command: --require=fixer.php Show all PHP errors: --debug Administer broken or slow sites
  16. What do you do repeatedly on your site that you

    ought to be able to automate? Interlude
  17. Aliases avoid the need to SSH into remote sites: wp

    @prod theme activate twenty seventeen wp @dev user create dan [email protected] wp @all core check-update Config: @trunk: path: /sites/wp/src @blog: ssh: [email protected]~/var/www/jb @dev: ssh: vagrant:default Administering remote sites
  18. Convert a site to HTTPS $ wp search-replace http://example.com https://example.com

    +------------------+-----------------------+--------------+------+ | Table | Column | Replacements | Type | +------------------+-----------------------+--------------+------+ | wp_commentmeta | meta_key | 0 | SQL | | wp_commentmeta | meta_value | 7 | SQL | | wp_comments | comment_author | 0 | SQL | | wp_comments | comment_author_email | 0 | SQL | | wp_options | option_name | 0 | SQL | | wp_options | option_value | 12 | PHP | | wp_options | autoload | 0 | SQL | | wp_postmeta | meta_key | 0 | SQL | | wp_postmeta | meta_value | 135 | PHP | ... Success: Made 271 replacements.
  19. Quickly connect to MySQL $ wp db cli MariaDB [trunk.wp]>

    show fields from wp_term_taxonomy; +------------------+---------------------+------+-----+---------+ | Field | Type | Null | Key | Default | +------------------+---------------------+------+-----+---------+ | term_taxonomy_id | bigint(20) unsigned | NO | PRI | NULL | | term_id | bigint(20) unsigned | NO | MUL | 0 | | taxonomy | varchar(32) | NO | MUL | | | description | longtext | NO | | NULL | | parent | bigint(20) unsigned | NO | | 0 | | count | bigint(20) | NO | | 0 | +------------------+---------------------+------+-----+---------+ 6 rows in set (0.00 sec)
  20. Blog via the CLI $ wp post create --post_title="Hello World"

    --edit // Opens your configured editor $ wp post edit 45
  21. Shell Friends $ wp post delete $(wp post list--format=ids) Success:

    Trashed post 64. Success: Trashed post 186. $ wp language core list --field=language | xargs -n 1 wp language core install Downloading translation from https://wordpress.org/.../af.zip... Success: Language installed. Downloading translation from https://wordpress.org/.../as.zip... Success: Language installed. Downloading translation from https://wordpress.org/.../az.zip... Success: Language installed. Downloading translation from https://wordpress.org/.../bel.zip... Success: Language installed. Downloading translation from https://wordpress.org/.../bg.zip... Success: Language installed. ...