Successfully reported this slideshow.
Your SlideShare is downloading. ×

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

Loading in …3

Check these out next

1 of 79 Ad

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

Download to read offline

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.

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.


More Related Content

Slideshows for you (20)

Similar to Take Command of WordPress With WP-CLI at WordCamp Long Beach (20)


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:

Editor's Notes

  • BEFORE WE START: Who is familiar with command line? Who is using WP-CLI?

    1) Lot of info
    2) Firehose
    Helicopter tour
    Points of interest
  • Professionally for 11 years
    Started with content and design, moving into development

  • 1. Intro
    2. Basic Installation
    3. Bulk of preso
    4. Not required by helpful
    5. Go over a couple packages to extend WP-CLI.
    6. Touch on Writing Commands
  • Stand-alone replacement for the GUI
    Except uploading files, anything in the GUI in the CLI
  • We have this GUI…

    1a. Compare GUI & CLI
    b. Bundle tasks together
    c. Use or write packages

    2a. Plugin, phpMyAdmin, hosting control panel
    b. Not switching taking up your time
  • 1. Windows support To borrow a phrase, “UNIX is a requirement that begins where requirement ends.”
    4. Choice of terminal apps.
  • Full-service hosting,
    Good news.
    Bad news.
  • a. Non-UNIX servers have own installations
    b. Can also install with Git, Composer, Homebrew, Docker
  • If you’re new to command line, good place to start.
    Get a feel for executing commands
    Learn how to know where you are when you can’t see your whole tree at once
    Won’t break anything.
    If you know WP, you know what these commands do. Low bar to entry. WP-CLI teaches you itself.
    Appreciate Hierarchical structure: wp = tool, command = area, subcommand =what you’re doing
    Flavor Efficiency: Compare examples with GUI
    Argument coming into play
  • List includes names, status (activated), update availability, version
    2. Like with themes…
    3. As we’ve seen, update…
  • One command to update all the thing

    Moving on…
  • 1. username, display name, email, reg date/time, role
    2. --porcelain suppressed password
    3. --yes suppresses confirmation messages
  • General, Reading, Writing
    1. Option and value
  • 2. String together commands you want to run after we’ve created a new WP install

  • Here we are passing data from one command to another. List of status=spam in the form of ids
  • 4. Last line to keep password from logging in bash history.

    ** What I was saying before about full-service hosts…
    You may not be able to create db or admin_password to txt file
    Your mileage may vary

    ? Anyone know of full-service host that allows creating db at CL
  • Note: ID, username or email.
  • Plugins remove load and security
  • 1. And assign capabilities as previous
    2. Delete role reverts user to previous role
    3. Reset changes to default capabilities
  • ***Themes, plugins, blocks, post types, taxonomies
  • Child theme: includes style.css, function.php (enqueues scripts and styles), .editorconfig
  • Same as
  • ** Creates a folder in Plugins folder
  • Supporting and testing files
  • ** Adds blocks/block-slug
    ** Adds post-types/post-type-slug.php
  • Create custom messages
  • Create custom messages
  • Create custom messages
  • Create custom messages
  • --post_types param
    ** Adds taxonomies/taxonomy-slug.php
  • Registering post type
  • Labels
  • Custom messages
  • If you want a subtler approach…
  • Use mysqlcheck utility
  • As we’ve seen
    … without opening a file
  • S/R will work on serialized data, but not inside a serialized object
  • List
    Think about Flush
  • Flush your blessed cache
    No, flush will not on CloudFlare
  • List: shows hook, time (local and GMT), and recurrence rate
  • Things I’m leaving out so there are some treasures you can find
  • Create staging/production pairs
    Create staging group and production group
  • 2. Update core, all themes, all plugins on all aliases
  • Command line configuration is stored in config.yml
    2. & 3. files inside the current working directory (or upwards)
    Fine-tune config to your needs

    But that’s not all
  • ***LIST!!!
  • ***LIST
  • Diagnoses problems within WordPress.
  • Checking security, performance, configuration
  • **Helps identify where WordPress is slow.
  • With spotlight, we’re getting four hooks of interest
  • Try your hand
  • We have a Scaffold Package command
    Keep track of your author/packagename to find it
  • Let me know how it goes