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

WP-CLI - How to manage WordPress from the comma...

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.

MarcelTannich

June 05, 2021
Tweet

More Decks by MarcelTannich

Other Decks in Programming

Transcript

  1. 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
  2. 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.
  3. 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
  4. 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
  5. 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
  6. 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/
  7. 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
  8. 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
  9. Help $ wp help Opens the general help $ wp

    help command Opens the help for the respective command (e.g. plugin)
  10. 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
  11. 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 [email protected] $ wp core install --prompt The identical command without parameters; prompts the user to enter values for all command arguments
  12. 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
  13. 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
  14. 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
  15. Plugins $ wp plugin update wordpress-seo Updates Yoast SEO $

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

    the name “Top Menu” $ wp menu location list Shows all menus
  17. 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
  18. 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
  19. Media $ wp media regenerate Regenerates thumbnails $ seq 500

    1000 | xargs wp media regenerate Regenerates thumbnails from IDs 500 - 1000
  20. 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
  21. 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”
  22. 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
  23. Database $ wp db export Exports the database as .sql

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

    wp language core install es_ES --activate Installs Spanish language file and activates the language
  25. 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