Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

The Themer's Guide to WP-CLI

342 views

Published on

Automate your theme building process on the command line and execute remote commands via aliases.

Published in: Technology
  • Be the first to comment

  • Be the first to like this

The Themer's Guide to WP-CLI

  1. 1. Themer’s Guide WordPress Command Line Interface __ _______ _____ _ _____ / / __ / ____| | |_ _| / / /| |__) |_____| | | | | | / / / | ___/______| | | | | | / / | | | |____| |____ _| |_ / / |_| _____|______|_____|
  2. 2. 1. Intro 2. WP-CLI Basics 3. Install WordPress 4. Plugin and Theme Automation 5. PHP7 Compatibility Checker 6. Advanced Agenda
  3. 3. _____ _ _ _ _ |_ _| | | | | | | (_) | | _ __ | |_ _ __ ___ __| |_ _ ___| |_ _ ___ _ __ | | | '_ | __| '__/ _ / _` | | | |/ __| __| |/ _ | '_ _| |_| | | | |_| | | (_) | (_| | |_| | (__| |_| | (_) | | | | |_____|_| |_|__|_| ___/ __,_|__,_|___|__|_|___/|_| |_|
  4. 4. •Sales Engineer - WP Engine •Developer, Freelancer •CMS Specialist •Open Source •Speaker Edmund Turbin WordCamp Stockholm - Nov 2016
  5. 5. THERE ARE TWO TYPES OF PEOPLE IN THE WORLD
  6. 6. I ❤
  7. 7. WHY USE COMMANDS? •Quickly execute a task •Bundle many tasks together in scripts •Avoid using the keyboard and mouse •Simplify repetitive tasks •and also…
  8. 8. WHY USE COMMANDS?SO WE CAN BE LAZY
  9. 9. ____ _ | _ (_) | |_) | __ _ ___ _ ___ ___ | _ < / _` | / __| | | / __| / __| | |_) | | (_| | __ | | | (__ __ |____/ __,_| |___/ |_| ___| |___/
  10. 10. WHAT DO I NEED TO GET STARTED? •Local Dev Environment with PHP - VVV, MAMP, WAMP •Terminal - Terminal App, iTerm 2, PuTTY •Text Editor - Sublime, Atom PREREQUISITES
  11. 11. INSTALL WP-CLI curl -O https://raw.githubusercontent.com/wp-cli/ builds/gh-pages/phar/wp-cli.phar https://make.wordpress.org/cli/handbook/installing/
  12. 12. INSTALL WP-CLI # Make file executable chmod +x wp-cli.phar # Move file to proper location sudo mv wp-cli.phar /usr/local/bin/wp https://make.wordpress.org/cli/handbook/installing/
  13. 13. INSTALL WP-CLI •Composer •Homebrew •Manually https://make.wordpress.org/cli/handbook/installing/ ALTERNATE INSTALL METHODS
  14. 14. INSTALL WP-CLI • change into a WP directory • run $wp cli info https://make.wordpress.org/cli/handbook/installing/ TEST THE INSTALLATION
  15. 15. COMMAND STRUCTURE $wp command subcommand value $wp plugin install wp-smushit
  16. 16. _____ __ _ _ _ / ____| / _(_) | | (_) | | ___ _ __ | |_ _ __ _ _ _ _ __ __ _| |_ _ ___ _ __ | | / _ | '_ | _| |/ _` | | | | '__/ _` | __| |/ _ | '_ | |___| (_) | | | | | | | (_| | |_| | | | (_| | |_| | (_) | | | | ________/|_| |_|_| |_|__, |__,_|_| __,_|__|_|___/|_| |_| __/ | |___/
  17. 17. WHAT HAVE I DONE? • Installed WP-CLI locally • Configured a WP-CLI alias to SSH into remotes • I can execute remote commands as if they were on my local machine • No need for local MySQL • Alternate - use SSH directly into VVV
  18. 18. CONFIGURATION •Alias called @dev allows me to run remote commands • $wp @dev cli info is different than $wp cli info • @dev connects via SSH and runs on Vagrant box https://make.wordpress.org/cli/handbook/config/ wp-cli.yml - Aliases with Vagrant
  19. 19. CONFIGURATION $ wp cli info PHP binary: /usr/bin/php PHP version: 5.5.38 php.ini used: WP-CLI root dir: phar://wp-cli.phar WP-CLI packages dir: /Users/edmund.turbin/.wp-cli/packages/ WP-CLI global config: WP-CLI project config: /Users/edmund.turbin/Development/vvv2/www/wpcli-test4/wp-cli.yml WP-CLI version:1.1.0 wp-cli.yml - Aliases with Vagrant
  20. 20. CONFIGURATION $wp @dev cli info PHP binary: /usr/bin/php7.0 PHP version: 7.0.17-2+deb.sury.org~trusty+1 php.ini used: /etc/php/7.0/cli/php.ini WP-CLI root dir: phar://wp-cli.phar WP-CLI packages dir: WP-CLI global config: WP-CLI project config: /srv/www/wpcli-test4/wp-cli.yml WP-CLI version:1.2.0-alpha-afe561b wp-cli.yml - Aliases with Vagrant
  21. 21. CONFIGURATION •Allows you to configure WP-CLI variables •File can be at the Global, Project and Local levels •e.g.: Local overrides Global •Allows you to set up Aliases per site https://make.wordpress.org/cli/handbook/config/ wp-cli.yml
  22. 22. CONFIGURATION @dev: ssh: vagrant@wpcli-test.dev/srv/www/wpcli-test https://make.wordpress.org/cli/handbook/installing/ wp-cli.yml - Aliases with Vagrant
  23. 23. _____ _ / ____| | | | | ___ _ __ ___ _ __ ___ __ _ _ __ __| | ___ | | / _ | '_ ` _ | '_ ` _ / _` | | '_ / _` | / __| | |____ | (_) | | | | | | | | | | | | | | (_| | | | | | | (_| | __ _____| ___/ |_| |_| |_| |_| |_| |_| __,_| |_| |_| __,_| |___/
  24. 24. CORE OPERATIONS •Downloads the recent version of WordPress to the current directory $wp core download
  25. 25. CORE OPERATIONS •Creates WordPress configuration file. •This file tells WP-CLI how connect to you’re site’s database $wp core config
  26. 26. CORE OPERATIONS •Completes the WordPress install process $wp core install
  27. 27. DATABASE OPERATIONS •Create a database for your WordPress install •Depends on a wp-config.php file to work $wp db create
  28. 28. INSTALL WORDPRESS # Download WordPress $wp core download # Create WordPress configuration - wp-config.php $wp core config —-dbuser=root --dbpass=root —-dbname=wpcli-test
  29. 29. INSTALL WORDPRESS # Create Database for install $wp db create wpcli-test # Run the install process $wp core install —url=wpcli-test.dev —-title=“wpcli test” —admin_user=admin --admin_password=password --admin_email=info@example.com
  30. 30. INSTALL WORDPRESS •Not needed if you’re using VVV •WP installs can be created and provisioned via Vagrant Note for VVV Users https://varyingvagrantvagrants.org/
  31. 31. _ _ _ / | | | | (_) / _ _ | |_ ___ _ __ ___ __ _ | |_ _ ___ _ __ / / | | | | | __| / _ | '_ ` _ / _` | | __| | | / _ | '_ / ____ | |_| | | |_ | (_) | | | | | | | | (_| | | |_ | | | (_) | | | | | /_/ _ __,_| __| ___/ |_| |_| |_| __,_| __| |_| ___/ |_| |_|
  32. 32. SHELL SCRIPTING •You can use WP CLI with like any other command •e.g. tie commands together and run them as a list
  33. 33. INSTALL WORDPRESS PLUGINS, THEME #!/bin/bash # list of plugins plugins=("user-switching" "debug-objects""wp-cfm") # loop through plugin list and install for plugin in ${plugins[*]}; do wp plugin install $plugin wp plugin activate $plugin done
  34. 34. INSTALL UNDERSCORE_S THEME echo "Enter site name:" read theme_name wp scaffold _s $theme_name wp theme activate $theme_name wp scaffold theme-tests $theme_name
  35. 35. GENERATE CONTENT wp post generate --count=10 wp post generate --count=10 --post_type=page wp comment generate --count=10
  36. 36. _____ _ _ _____ ______ _____ _ _ | __ | | | | | __ |____ | / ____| | | | | | |__) | | |__| | | |__) | / / | | | |__ ___ ___ | | __ | ___/ | __ | | ___/ / / | | | '_ / _ / __| | |/ / | | | | | | | | / / | |____ | | | | | __/ | (__ | < |_| |_| |_| |_| /_/ _____| |_| |_| ___| ___| |_|_
  37. 37. PHP COMPATIBILITY COMMAND •Run PHP7 Compatibility Checker •Plugin needs to be installed •Version supplied as an argument wp phpcompat 7.0
  38. 38. RUN PHP COMPATIBILITY CHECK SCRIPT #!/bin/bash wp plugin install php-compatibility-checker wp plugin activate php-compatibility-checker wp phpcompat 7.0 > php-compat-results.txt
  39. 39. PLUGINS WITH COMMANDS •ACF •Elastic press •Migrate, migrated •caching plugins •PHP Compatibility •Regen thumbs •widget import/export •developer •wp-cfm •backupwordpress https://make.wordpress.org/cli/handbook/tools/
  40. 40. _ _ / | | | | / __| | __ __ __ _ _ __ ___ ___ __| | / / / _` | / / / _` | | '_ / __| / _ / _` | / ____ | (_| | V / | (_| | | | | | | (__ | __/ | (_| | /_/ _ __,_| _/ __,_| |_| |_| ___| ___| __,_|
  41. 41. NOTABLE COMMANDS •Scaffold - create plugins, child themes, unit tests •db, db-query •transient •wp-cfm - configuration management •backupwordpress https://make.wordpress.org/cli/handbook/tools/
  42. 42. SHELL ALIAS COMMAND Create a shortcut command for things that are frequently used to cut down on keystrokes. alias - show all aliases
  43. 43. SHELL ALIASES Create an alias alias pu=“wp plugin update --all”
  44. 44. SHELL ALIASES Remove an alias unalias pu
  45. 45. SHELL ALIASES Arguments alias pi='wp plugin install’ pi wp-smushit
  46. 46. SHELL ALIASES Aliases can be saved and managed from .bash_profile
  47. 47. CUSTOM SHELL COMMANDS • functions can be created in your shell profile (.bash_profile) and executed globaly wp_info () { wp cli info }
  48. 48. Helpful Links Command Cookbook https://make.wordpress.org/cli/handbook/commands-cookbook/ Shell Friends https://make.wordpress.org/cli/handbook/shell-friends/ Plugins that work with WP-CLI https://make.wordpress.org/cli/handbook/tools/ WP-CLI Configuration https://make.wordpress.org/cli/handbook/config/ Shell Scripting https://www.shellscript.sh/
  49. 49. WP-CLI is a toolbox Commands can be linked together via scripts to automate complex tasks
  50. 50. WP-CLI for Themeing WP-CLI can be one of many tools to get your development process started quickly
  51. 51. Good, Better, Best •Single Commands •Shell Scripts •Shell Commands
  52. 52. Thank you! @spicecadet edmund.turbin@wpengine.com

×