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

61 views

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.com @likethegoddess hi@diana.fyi
  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 https://make.wordpress.org/cli/handbook/hosting-companies/
  10. 10. WP-CLI Installation 1) Download wp-cli.phar $ curl -O https://raw.githubusercontent.com/wp-cli/builds/ 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 https://make.wordpress.org/cli/handbook/installing/
  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) https://wp-cli.org/#tab-completions
  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 https://make.wordpress.org/cli/handbook/config/#global-parameters
  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="user@email.com" https://developer.wordpress.org/cli/commands/core/
  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 https://developer.wordpress.org/cli/commands/plugin/
  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 https://developer.wordpress.org/cli/commands/theme/
  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 ann@example.com --porcelain --send-email Delete user $ wp user delete john --reassign=lynn $ wp user delete john --yes https://developer.wordpress.org/cli/commands/user/
  20. 20. Options List site options $ wp option list Change site URL $ wp option update siteurl http://new.siteurl.com Change admin email $ wp option update admin_email admin@siteurl.com https://developer.wordpress.org/cli/commands/option/
  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 https://codex.wordpress.org/Option_Reference/
  22. 22. Comments Delete all spam comments $ wp comment delete $(wp comment list --status=spam --format=ids) https://developer.wordpress.org/cli/commands/comment/
  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 https://developer.wordpress.org/cli/commands/site/
  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 https://developer.wordpress.org/cli/commands/super-admin/
  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 --url=example.com --title="Site Title" --admin_user=username --admin_email=admin@example.com --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 john@smith.com publish_newsletters https://developer.wordpress.org/cli/commands/user/
  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 https://developer.wordpress.org/cli/commands/cap/
  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 https://developer.wordpress.org/cli/commands/role/
  31. 31. Posts and Pages Generate posts $ wp post generate --count=10 Generate pages with lorem ipsum $ curl http://loripsum.net/api/5 | wp post generate --post_type=page --count=10 https://developer.wordpress.org/cli/commands/post/
  32. 32. Comments Generate comments $ wp comment generate --format=ids --count=3 --post_id=123 https://developer.wordpress.org/cli/commands/comment/
  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" --author_uri=myurl.com --theme_uri=mythemeurl.com --activate https://developer.wordpress.org/cli/commands/scaffold/child-theme/
  35. 35. Scaffold UnderscoresTheme Create theme based on Underscores $ wp scaffold _s my-theme-slug --theme_name="My Theme Name" --author="My Name" --author_uri=myurl.com --sassify --woocommerce --activate https://developer.wordpress.org/cli/commands/scaffold/underscores/
  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" --plugin_author_uri=myurl.com --plugin_uri=mypluginurl.com https://developer.wordpress.org/cli/commands/scaffold/plugin/
  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 install-wp-tests.sh  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 https://developer.wordpress.org/cli/commands/scaffold/block/
  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 https://developer.wordpress.org/cli/commands/scaffold/post-type/
  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" https://developer.wordpress.org/cli/commands/scaffold/taxonomy
  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 https://developer.wordpress.org/cli/commands/db/
  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 https://developer.wordpress.org/cli/commands/site/
  45. 45. DatabaseWith mysqlcheck Check database $ wp db check Optimize database $ wp db optimize Repair database $ wp db repair https://dev.mysql.com/doc/refman/8.0/en/mysqlcheck.html
  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 https://developer.wordpress.org/cli/commands/config/
  47. 47. Search and Replace Search for staging URL and replace with production URL $ wp search-replace 'http://example.dev' 'http://example.com' --dry-run Search/replace to a SQL file without transforming the database $ wp search-replace 'http://example.dev' 'http://example.com' --export=database.sql Run case-insensitive regex search/replace operation $ wp search-replace '[foo id="([0-9]+)"' '[bar id="1"' -- regex --regex-flags='i' https://developer.wordpress.org/cli/commands/search-replace/
  48. 48. Rewrite List rewrite rules $ wp rewrite list --format=csv Update permalink structure $ wp rewrite structure '/%postname%' Flush rewrite rules $ wp rewrite flush https://developer.wordpress.org/cli/commands/rewrite/
  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 https://developer.wordpress.org/cli/commands/cache/
  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 https://developer.wordpress.org/cli/commands/transient/
  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 https://developer.wordpress.org/cli/commands/cron/
  52. 52. More Commands within plugins Wrappers Editor plugins Vagrant boxes https://make.wordpress.org/cli/handbook/tools/
  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 https://developer.wordpress.org/cli/commands/cli/alias/
  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 https://make.wordpress.org/cli/handbook/config/
  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 https://make.wordpress.org/cli/handbook/config/
  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 https://make.wordpress.org/cli/handbook/shell-friends/
  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 https://developer.wordpress.org/cli/commands/package/
  61. 61. Doctor Install $ wp package install git@github.com:wp-cli/doctor-command.git 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 https://github.com/wp-cli/doctor-command
  62. 62. Profile Install $ wp package install git@github.com:wp-cli/profile-command.git Run command $ wp profile stage $ wp profile hook $ wp profile eval $ wp profile eval-file https://github.com/wp-cli/profile-command/
  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 https://developer.wordpress.org/cli/commands/profile/hook/
  64. 64. WRITING COMMANDS Make Your Own Packages
  65. 65. Scaffold Package 1) Install Scaffold Package Command $ wp package install git@github.com:wp-cli/scaffold-package- command.git 2) Generate files for a basic WP-CLI command $ wp scaffold package author/packagename --description="My Description" --homepage=package-homepage.com --dir=package-dir 3) Find your package /home/usr/.wp-cli/packages/local/author/packagename https://github.com/wp-cli/scaffold-package-command/
  66. 66. Scaffolded Package Content  .github  bin  features  utils .distignore .editorconfig .gitignore .travis.yml command.php composer.json CONTRIBUTING.md README.md 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 https://wp-cli.org/
  69. 69. THANKS! Diana Thompson hi@diana.fyi @likethegoddess Slides: diana.fyi/wp-cli

×