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

WP-CLI - How to manage WordPress from the command-line

WP-CLI - How to manage WordPress from the command-line

WP-CLI is the official command-line interface for WordPress. It provides a collection of commands that can be used to perform various WordPress actions such as plugin & theme installations, core updates, configurations or backups without using a browser.

These slides were created for a workshop at WordCamp Europe 2021 and are aimed at beginner & advanced WordPress users who are not familiar with the command line yet and are looking for an easy way to get started.

A3b826349b841aaed8830491ee9cd5df?s=128

MarcelTannich

June 05, 2021
Tweet

More Decks by MarcelTannich

Other Decks in Programming

Transcript

  1. WP-CLI - How to manage WordPress from the command-line Marcel

    Tannich | WordCamp Europe 2021
  2. Marcel Tannich WordPress Technical Supporter @ OnTheGoSystems

  3. Download Slides https://github.com/marceltannich/wceu_2021 https://www.marceltannich.com

  4. What is the command-line interface? • A text-based application to

    view, edit and manipulate your files • Also known as console, terminal, CMD, CLI or prompt • Due to their close relation, these terms are used synonymously • A ">" or "$" character signals that input is being waited for
  5. None
  6. What is WP-CLI? • WP-CLI is a collection of command-line

    tools for managing your WordPress installations. • You can use it to perform various WordPress actions without using a browser. • The goal is to provide a command-line interface for every action on WordPress as an alternative to the backend.
  7. None
  8. And what can I do with it? • Manage WordPress

    installations (updates, installations, settings, ...) • Add/remove users, posts or categories • Insert Dummy Data • Perform search & replace in the database • Reset passwords • Import/export Backups • Generate code boilerplates for plugins, themes & blocks • Create scripts for automation
  9. Save time and be more efficient!

  10. Requirements WP-CLI • Installation on your server / localhost •

    SSH access to your hosting package / server • UNIX like environment (macOS, FreeBSD, Cygwin, ...) or Windows (limited) • PHP 5.6 or later • WordPress 3.7 or later
  11. Installation

  12. Installation WP-CLI Windows 10 • Download .phar file • Create

    folder • Install local server e.g.: XAMPP • Set environment variable • Create batch file • https://www.youtube.com/watch?v=sd6MWc29G84
  13. Installation WP-CLI macOS / Linux • $ curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/

    phar/wp-cli.phar (or wget) • $ php wp-cli.phar --info • $ chmod +x wp-cli.phar • $ sudo mv wp-cli.phar /usr/local/bin/wp • $ wp --info • https://wpbeaches.com/installing-wordpress-wp-cli-macos/ • https://www.linode.com/docs/websites/cms/install-wordpres s-using-wp-cli-on-ubuntu-18-04/
  14. WP-CLI pre-installed by default at: • Cloudways • SiteGround •

    Bluehost • Dreamhost • Synthesis • WordPress.com VIP • Raidboxes • WPEngine • IONOS • The full list is available in the WP-CLI manual
  15. WP-CLI connection

  16. WP-CLI connection • The connection is made via the Secure

    Shell Protocol (SSH) • Ask your web host for help • Connection is made via terminal (Mac, Linux) • Windows do not support SSH via CMD; use Putty, GitBash, Cygwin or OpenSSH
  17. Commands https://developer.wordpress.org/cli/commands/

  18. Kommandos

  19. Global Parameters • --prompt • --quiet • --path Command arguments

    • --dry-run for search-replace
  20. Help $ wp help Opens the general help $ wp

    help command Opens the help for the respective command (e.g. plugin)
  21. Core $ wp core download Downloads the WordPress core $

    wp core version Shows the current WordPress version $ wp core update Updates the WordPress core to a newer version
  22. Core $ wp core install Starts the standard WordPress installation

    process $ wp core install --url=mysite.server.com --title=title --admin_user=you --admin_password=Password1 --admin_email=your@email.address $ wp core install --prompt The identical command without parameters; prompts the user to enter values for all command arguments
  23. Set WordPress options $ wp option update blogname “My awesome

    website” Updates your blog title wp option update blogdescription "This is my website" Updates your blog description
  24. Themes $ wp theme install astra --activate Installs & activates

    the Astra Theme $ wp theme activate astra If Astra Theme is already installed you can activate it using the “activate” sub command $ wp theme update astra Updates a theme
  25. Plugins $ wp plugin install wordpress-seo --activate Installs Yoast SEO

    and activates the plugin. $ wp plugin install duplicate-post woocommerce elementor contact-form-7 --activate Installs several plugins in bulk and activates them
  26. Plugins $ wp plugin update wordpress-seo Updates Yoast SEO $

    wp plugin update --all Updates all plugins
  27. Menus $ wp menu create “Top-Menu” Creates a menu with

    the name “Top Menu” $ wp menu location list Shows all menus
  28. Maintenance mode $ wp maintenance-mode activate Updates Activates the WordPress

    maintenance mode $ wp maintenance-mode status Shows the current status $ wp maintenance-mode deactivate Deactivates the maintenance mode
  29. Search & Replace $ wp search-replace “old string” “new string”

    Searches all rows for a string and replaces it $ wp search-replace “dev.test” “mywebseite.com” Useful during a database migration to change the site URL $ wp search-replace “dev.test” “mywebseite.com” --dry-run Tip: Always use --dry-run first; this shows the output without writing anything in the database
  30. Cache $ wp cache flush Clears the WordPress object cache

  31. Media $ wp media regenerate Regenerates thumbnails $ seq 500

    1000 | xargs wp media regenerate Regenerates thumbnails from IDs 500 - 1000
  32. User Management $ wp user create Creates a new WordPress

    user and shows all available parameters $ wp user create --prompt Tip: Use the global parameter --prompt to not have to remember the specific parameters
  33. User Management $ wp user add-role 2 administrator Assigns admin

    rights to the user with ID “2” $ wp user get 2 Returns details about the user with ID “2” $ wp user delete 2 --reassign=1 Deletes the user with ID “2” and reassign all posts to user with ID “1”
  34. Posts $ wp post generate Generates 100 dummy posts $

    wp post generate --count=10 --post_date=2021_06_15 Generates 10 Posts with date 2021-06-15 $ curl -N http://loripsum.net/api/5 | wp post generate --post_content --count=10 Generates 10 posts with 5 paragraphs of Lorem Ipsum text
  35. Database $ wp db export Exports the database as .sql

    file $ wp db import database.sql Imports the database
  36. Languages $ wp language core list Shows all available languages

    wp language core install es_ES --activate Installs Spanish language file and activates the language
  37. Scaffold $ wp scaffold _s my-theme --theme_name="Test" --author="Marcel" Creates a

    Custom Theme based on Underscores (_s) $ wp scaffold plugin my-plugin Creates a plugin boilerplate $ wp scaffold block team --title="Team" --theme=astra Creates a new "Team" block for the "Astra" theme
  38. WP-CLI is extendable! $ wp package install

  39. WP-CLI can be automated via scripting! https://make.wordpress.org/cli/handbook/ commands-cookbook/

  40. Thank you for your attention! Questions? @MarcelTannich