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.

Take Command of WordPress With WP-CLI at WordCamp Long Beach


Published on

WP-CLI is the official command line tool for interacting with and managing your WordPress sites. Learn to use it to speed up maintenance and deployment tasks, and to aid in theme and plugin development.

Published in: Internet
  • Be the first to comment

  • Be the first to like this

Take Command of WordPress With WP-CLI at WordCamp Long Beach

  1. 1. Take Command ofWordPressWith WP-CLI Diana Thompson WordCamp Long Beach October 5, 2019
  2. 2. Speaker Diana Thompson Pronouns: she/her Like the Goddess @likethegoddess
  3. 3. Agenda 1. WP-CLI: What and Why 2. Getting Started 3. Commands a. Mirroring the GUI b. Behind the Scenes 4. Configuration 5. Packages 6. Writing Commands
  4. 4. What isWP-CLI? WP-CLI is the official command line interface for WordPress. What is a command line interface? A command-line interface…is a means of program where the user issues commands to the of successive lines of text. (Wikipedia)
  5. 5. WhyWP-CLI? 1. Execute tasks faster 2. Get more capabilities with the CLI
  6. 6. WhyWP-CLI? Do more in less time.
  7. 7. GETTING STARTED Let’s Go!
  8. 8. Requirements UNIX-like server environment PHP 5.4 or later WordPress 3.7 or later Terminal app: native apps, PuTTY SSH access
  9. 9. Web HostsWithWP-CLI Installed Bluehost Dreamhost HostGator SiteGround WPEngine among others
  10. 10. WP-CLI Installation 1) Download wp-cli.phar $ curl -O gh-pages/phar/wp-cli.phar 2) Confirm it works $ php wp-cli.phar --info 3) Enable use of wp instead of php wp-cli.phar $ chmod +x wp-cli.phar $ sudo mv wp-cli.phar /usr/local/bin/wp 4) Confirm successful installation $ wp --info
  11. 11. Navigation pwd print working directory ls list segments (non-OS contents) cd change directory ../ parent directory / system root - last directory ~ home directory Up and down arrow keys to review and execute previous commands Tab to autocomplete options (installation required)
  12. 12. WP-CLI Syntax $ wp command subcommand [value] [--argument] Examples: $ wp core download $ wp plugin update --all $ wp theme install twentynineteen --activate
  13. 13. Global Parameters Perform operation against a remote server over SSH --ssh=[<scheme>:][<user>@]<host|container>[:<port>][<path>] Path to the WordPress files --path=<path> Suppress informational messages --quiet Prompt the user to enter values for all arguments --prompt
  14. 14. COMMANDS Mirroring the GUI
  15. 15. Core Check for available core update $ wp core check-update Update WordPress $ wp core update Convert to Multisite $ wp core multisite-convert Install Multisite $ wp core multisite-install --title="Welcome to the WordPress" > --admin_user="username" --admin_password="password" > --admin_email=""
  16. 16. Plugins List plugins $ wp plugin list Install and activate a plugin from wp plugin repository $ wp plugin install akismet –-activate Update plugins $ wp plugin update akismet $ wp plugin update akismet wp-super-cache woocommerce $ wp plugin update --all
  17. 17. Themes List themes $ wp theme list Install and activate a theme from wp theme repository $ wp theme install twentynineteen --activate Update themes $ wp theme update twentynineteen $ wp theme update twentynineteen twentyseventeen twentysixteen $ wp theme update --all
  18. 18. Combine Commands with Pipes Update core, all plugins, and all themes $ wp core update | wp plugin update --all | wp theme update --all
  19. 19. Users List users $ wp user list Create user $ wp user create ann --porcelain --send-email Delete user $ wp user delete john --reassign=lynn $ wp user delete john --yes
  20. 20. Options List site options $ wp option list Change site URL $ wp option update siteurl Change admin email $ wp option update admin_email
  21. 21. Options Change image thumbnail size $ wp option update thumbnail_size_w 200 | wp option update thumbnail_size_h 200 | $ wp media regenerate --yes Change permalink structure, change time zone, block search engines, and close commenting $ wp option update permalink_structure /%postname%/ | wp option update gmt_offset -7 | $ wp option update blog_public 0 | $ wp option update default_comment_status closed
  22. 22. Comments Delete all spam comments $ wp comment delete $(wp comment list --status=spam --format=ids)
  23. 23. Multisite
  24. 24. Site for Multisite Installations List sites $ wp site list Create a site $ wp site create --slug=example Deactivate a site $ wp site deactivate 123 Archive a site $ wp site archive 123
  25. 25. SuperAdmin List super admins $ wp super-admin list Add a super admin $ wp super-admin add username Remove a super admin $ wp super-admin remove username
  26. 26. COMMANDS Behind the Scenes
  27. 27. WordPress Installation 1. wp db create –-dbuser=db-username --dbpass=db-password 2. wp core download 3. wp config create --dbname=database-name --dbuser=dbuser --dbpass=db-password --dbhost=hostname --dbprefix=prfx_ 4. wp core install --title="Site Title" --admin_user=username --prompt=admin_password < admin_password.txt
  28. 28. User Capabilities List a user’s capabilities $ wp user list-caps 21 Add a capability to user $ wp user add-cap johnsmith edit_product Remove a capability to user $ wp user remove-cap publish_newsletters
  29. 29. Role Capabilities List the capabilities of a role, sorted alphabetically $ wp cap list 'author' | sort Add a capability to a role $ wp cap add author spectate Remove a capability from a role $ wp cap remove author spectate
  30. 30. Roles Create a role $ wp role create productadmin "Product Administrator" Delete a role $ wp role delete productadmin Reset roles $ wp role reset administrator $ wp role reset administrator author contributor $ wp role reset --all
  31. 31. Posts and Pages Generate posts $ wp post generate --count=10 Generate pages with lorem ipsum $ curl | wp post generate --post_type=page --count=10
  32. 32. Comments Generate comments $ wp comment generate --format=ids --count=3 --post_id=123
  33. 33. Scaffold
  34. 34. Scaffold ChildTheme Create child theme $ wp scaffold child-theme my-child-theme-slug --parent_theme=parent-theme-slug --theme_name="My Child Theme Name" --author="My Name" --activate
  35. 35. Scaffold UnderscoresTheme Create theme based on Underscores $ wp scaffold _s my-theme-slug --theme_name="My Theme Name" --author="My Name" --sassify --woocommerce --activate
  36. 36. Scaffold Plugins Generate starter code of a plugin $ wp scaffold plugin my-plugin-slug --dir=path/to/plugins --plugin_name="My Plugin Name" --plugin_description="My Plugin Description" --plugin_author="My Name"
  37. 37. Scaffolded Plugin Contents  bin  tests .distignore .editorconfig .gitignore .phpcs.xml.dist .travis.yml Gruntfile.js my-plugin-slug.php package.json phpunit.xml.dist readme.txt  bin  tests bootstrap.php test-sample.php
  38. 38. Scaffold Blocks Generate a block for a theme $ wp scaffold block my-block-slug --title="My Block Title" --theme=theme-slug Generate a block for a plugin $ wp scaffold block my-block-slug --title="My Block Title" --plugin=plugin-slug
  39. 39. Scaffolded Block Contents  my-block-slug my-block-slug.php  my-block-slug editor.css index.js style.css
  40. 40. Scaffold PostTypes Generate custom post type $ wp scaffold post-type my-post-type-slug --label="My Post Type" --textdomain="my-post-type" Generate custom post type for a theme $ wp scaffold post-type my-post-type-slug --label="My Post Type" --theme=my-theme Generate custom post type for a plugin $ wp scaffold post-type my-post-type-slug --label="My Post Type" --plugin=my-plugin
  41. 41. ScaffoldTaxonomies Generate taxonomy $ wp scaffold taxonomy taxonomy-slug --textdomain="taxonomy- slug" Generate taxonomy for a theme $ wp scaffold taxonomy taxonomy-slug --post_types=my-post- type-slug > taxonomy.php --theme="twentynineteen" Generate taxonomy for a plugin $ wp scaffold taxonomy taxonomy-slug --post_types=my-post- type-slug > taxonomy.php --plugin="custom-plugin"
  42. 42. Database Config, Search/Replace, and More
  43. 43. Database Create database $ wp db create Import database $ wp db import wpdb.sql Export database $ wp db export Reset current database $ wp db reset --yes Delete existing database $ wp db drop --yes
  44. 44. Site Remove pages, posts, and custom post types $ wp site empty --yes Remove pages, posts, custom post types, and uploads $ wp site empty --uploads --yes
  45. 45. DatabaseWith mysqlcheck Check database $ wp db check Optimize database $ wp db optimize Repair database $ wp db repair
  46. 46. Config Create wp-config.php $ wp config create --dbname=database-name --dbuser=db-user --dbpass=db-password --dbhost=hostname --dbprefix=prfx_ Set constants and variables $ wp config set WP_DEBUG true --raw Change salt keys $ wp config shuffle-salts
  47. 47. Search and Replace Search for staging URL and replace with production URL $ wp search-replace '' '' --dry-run Search/replace to a SQL file without transforming the database $ wp search-replace '' '' --export=database.sql Run case-insensitive regex search/replace operation $ wp search-replace '[foo id="([0-9]+)"' '[bar id="1"' -- regex --regex-flags='i'
  48. 48. Rewrite List rewrite rules $ wp rewrite list --format=csv Update permalink structure $ wp rewrite structure '/%postname%' Flush rewrite rules $ wp rewrite flush
  49. 49. Object Cache Set cache $ wp cache set my_key my_value my_group 300 Increase cache value $ wp cache incr my_key 2 my_group Decrease cache value $ wp cache decr my_key 2 my_group Flush cache $ wp cache flush
  50. 50. Transient Cache Set transient $ wp transient set my_key "test data" 3600 Delete transients $ wp transient delete my_key $ wp transient delete --expired $ wp transient delete --all
  51. 51. WP-Cron List scheduled cron events $ wp cron event list Schedule a new cron event $ wp cron event schedule cron_test Test that cron runs successfully $ wp cron test
  52. 52. More Commands within plugins Wrappers Editor plugins Vagrant boxes
  53. 53. CONFIGURATION For Efficiency and Specificity
  54. 54. CLI Aliases Add alias $ wp cli alias add @staging --set-ssh=login@host --set- path=/path/to/wp/install/ --set-user=wpcli $ wp cli alias add @production --set-ssh=login@host --set- path=/path/to/wp/install/ --set-user=wpcli Create an alias group $ wp cli alias add @ourservers --grouping=staging,production
  55. 55. Executing CommandsWith Aliases Update staging and production $ wp core update @staging $ wp core update @production $ wp core update @ourservers $ wp core update @all Update all sites $ wp core update @all | wp theme update --all @all | wp plugin update --all @all
  56. 56. Creating Aliases in Config Files @staging: ssh: username@hostname user: username path: /path/to/staging/install/ @production: ssh: username@hostname user: username path: /path/to/production/install/ @ourservers: - @staging - @production
  57. 57. Configuration Files Arguments are interpreted in the following order: 1. Command-line arguments 2. wp-cli.local.yml 3. wp-cli.yml 4. ~/.wp-cli/config.yml 5. WP-CLI defaults
  58. 58. Shortening Commands with Bash Update all sites $ wp core update @all | wp theme update --all @all | wp plugin update --all @all Create bash alias $ alias update-all='wp core update @all && wp plugin update -- all @all && wp theme update --all @all' Update core, themes, and plugins on all aliases $ update-all
  59. 59. PACKAGES Extend WP-CLI
  60. 60. Packages List installed packages $ wp package list Install a package $ wp package install package-slug Update a package $ wp package update package-slug Uninstall a package $ wp package uninstall package-slug
  61. 61. Doctor Install $ wp package install Get list of checks wp doctor can perform $ wp doctor list Perform checks $ wp doctor check core-verify-checksums $ wp doctor check core-verify-checksums file-eval php-in-uploads $ wp doctor check --all
  62. 62. Profile Install $ wp package install Run command $ wp profile stage $ wp profile hook $ wp profile eval $ wp profile eval-file
  63. 63. Profile Hook Profile all hooks $ wp profile hook Profile the wp_head hook $ wp profile hook wp_head Profile all hooks with zero-ish values eliminated $ wp profile hook --spotlight
  64. 64. WRITING COMMANDS Make Your Own Packages
  65. 65. Scaffold Package 1) Install Scaffold Package Command $ wp package install command.git 2) Generate files for a basic WP-CLI command $ wp scaffold package author/packagename --description="My Description" --dir=package-dir 3) Find your package /home/usr/.wp-cli/packages/local/author/packagename
  66. 66. Scaffolded Package Content  .github  bin  features  utils .distignore .editorconfig .gitignore .travis.yml command.php composer.json wp-cli.yml
  67. 67. HELP Is Available
  68. 68. Resources Within WP-CLI $ wp help $ wp help <command> $ wp help <command> <subcommand> On the Web
  69. 69. THANKS! Diana Thompson @likethegoddess Slides: