How to use WP-CLI to manage WordPress sites quickly and efficiently
WP-CLI is a powerful command-line tool that allows managing WordPress installations directly from the terminal. This tool can save developers and site administrators a lot of time and effort, especially when dealing with multiple installations or performing complex tasks. In this post, we'll cover the basics of working with WP-CLI, as well as some of the most useful commands you can use to manage your WordPress sites.
1. Install and setup WP-CLI
To get started with WP-CLI, you must first install it on your system. Here are the steps to install WP-CLI:
- Open a terminal (on Mac/Linux) or command window (on Windows)
- Run the following command:
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
- After downloading
wp-cli.phar
, make the file executable with this command:
chmod +x wp-cli.phar
- Move
wp-cli.phar
in a directory that is included in the systemPATH
, so you can usewp
command from anywhere. For example:
sudo mv wp-cli.phar /usr/local/bin/wp
After installing WP-CLI, you can check the version and make sure the installation was successful by running the following command:
wp --info
2. Basic commands and management of WordPress sites
After you have successfully installed WP-CLI, you can start using its versatile commands to manage your WordPress sites. Here are some basic commands to get you started:
- Update WordPress:
wp core update
- Install theme:
wp theme install
- Activate a theme:
wp theme activate
- Disable a theme:
wp theme deactivate
- Install Plugin:
wp plugin install
- Activating a plugin:
wp plugin activate
- Disable plugin:
wp plugin deactivate
- Update Plugins:
wp plugin update --all
- Create a new user:
wp user create --role=
- Change a user's password:
wp user update --user_pass=
3. Working with the database and migrations
WP-CLI offers powerful tools for working with the database of your WordPress sites, allowing you to backup, restore, import and export data, as well as execute SQL queries directly from the command line. Here are some useful commands for working with databases:
- Export the database:
wp db export
- Import the database:
wp db import
- Database Optimization:
wp db optimize
- Executing an SQL query:
wp db query " "
4. Automate tasks using WP-CLI
WP-CLI can be used to automate routine tasks such as updating WordPress themes, plugins, and core, clearing the cache, or generating database backups. You can create scripts that use WP-CLI commands and set them to run automatically at certain intervals using your operating system's system task scheduler (cron).
WP-CLI is a powerful and flexible tool that can significantly improve the efficiency of managing WordPress sites. With its help, you can perform a wide range of tasks, such as installing and updating themes and plugins, managing users, working with the database, and automating routine tasks. Mastering WP-CLI can save you time and effort, allowing you to focus on the essential aspects of your work as a WordPress site developer or administrator.