WP-CLI (WordPress Command Line Interface) is an essential tool for WordPress developers that allows you to manage WordPress installations from the terminal. Whether you're installing plugins, managing users, or updating WordPress core, WP-CLI makes tasks faster and more efficient.
Installing WP-CLI
The great thing about WP-CLI is that it's already installed on most hosting companies that support WordPress. If you're using LocalWP for local development, WP-CLI comes pre-installed, so you can start using it right away!
Manual Installation Steps
If WP-CLI is not installed on your system, you can follow these steps to install it manually:
- Download WP-CLI:
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
- Make the file executable:
chmod +x wp-cli.phar
- Move it to a global location:
sudo mv wp-cli.phar /usr/local/bin/wp
- Verify the installation:
wp --info
If installed correctly, you will see details about WP-CLI, PHP version, and OS.
Checking WP-CLI Installation
To check if WP-CLI is installed, open your terminal and run:
wp --info
If WP-CLI is installed, you'll see output similar to this:
OS: Linux 5.15.0-56-generic
Shell: /bin/bash
PHP: 7.4.33
WP-CLI: 2.8.0
If WP-CLI is not installed on your system or hosting environment, you can manually install it by following the instructions above or on the official WP-CLI website.
Common WP-CLI Commands
Once WP-CLI is set up, you can start using it to manage your WordPress site. Here are some common commands:
1. Install WordPress
wp core download
wp config create --dbname=wordpress --dbuser=root --dbpass=root
wp db create
wp core install --url="http://example.com" --title="My Site" --admin_user="admin" --admin_password="password" --admin_email="[email protected]"
2. Managing Plugins
- Install a plugin:
wp plugin install woocommerce --activate
- Update all plugins:
wp plugin update --all
- Deactivate a plugin:
wp plugin deactivate contact-form-7
3. Managing Themes
- Install and activate a theme:
wp theme install astra --activate
- List installed themes:
wp theme list
4. Database Management
- Export the database:
wp db export backup.sql
- Import a database backup:
wp db import backup.sql
- Optimize the database:
wp db optimize
Automating My WordPress Setup with WP-CLI
As a freelance WordPress developer, I use WP-CLI in almost all my projects to speed up my workflow. To make things even faster, I have prepared a script that installs the most common plugins I use in my projects. This helps me quickly set up WordPress and start working on customization right away.
Here’s a simple version of my script:
#!/bin/bash
# Install essential plugins
PLUGINS=(
"woocommerce"
"elementor"
"contact-form-7"
"wpforms-lite"
"seo-by-rank-math"
"wp-super-cache"
)
for PLUGIN in "${PLUGINS[@]}"; do
wp plugin install $PLUGIN --activate
done
echo "All essential plugins installed and activated!"
Conclusion
WP-CLI is a game-changer for WordPress developers, allowing us to manage WordPress sites more efficiently. Whether you're working locally with LocalWP or on a hosting provider that supports WP-CLI, you can streamline your development process and save valuable time. If you're a freelancer like me, automating your setup with WP-CLI scripts can make project creation even faster and more consistent.
If you haven't started using WP-CLI yet, I highly recommend giving it a try!