DEV Community

Cover image for Getting Started with WP-CLI: A Powerful Tool for WordPress Developers
Muhammad Medhat
Muhammad Medhat

Posted on

Getting Started with WP-CLI: A Powerful Tool for WordPress Developers

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:

  1. Download WP-CLI:
 curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar 
Enter fullscreen mode Exit fullscreen mode
  1. Make the file executable:
 chmod +x wp-cli.phar 
Enter fullscreen mode Exit fullscreen mode
  1. Move it to a global location:
 sudo mv wp-cli.phar /usr/local/bin/wp 
Enter fullscreen mode Exit fullscreen mode
  1. Verify the installation:
 wp --info 
Enter fullscreen mode Exit fullscreen mode

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 
Enter fullscreen mode Exit fullscreen mode

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 
Enter fullscreen mode Exit fullscreen mode

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="admin@example.com" 
Enter fullscreen mode Exit fullscreen mode

2. Managing Plugins

  • Install a plugin:
 wp plugin install woocommerce --activate 
Enter fullscreen mode Exit fullscreen mode
  • Update all plugins:
 wp plugin update --all 
Enter fullscreen mode Exit fullscreen mode
  • Deactivate a plugin:
 wp plugin deactivate contact-form-7 
Enter fullscreen mode Exit fullscreen mode

3. Managing Themes

  • Install and activate a theme:
 wp theme install astra --activate 
Enter fullscreen mode Exit fullscreen mode
  • List installed themes:
 wp theme list 
Enter fullscreen mode Exit fullscreen mode

4. Database Management

  • Export the database:
 wp db export backup.sql 
Enter fullscreen mode Exit fullscreen mode
  • Import a database backup:
 wp db import backup.sql 
Enter fullscreen mode Exit fullscreen mode
  • Optimize the database:
 wp db optimize 
Enter fullscreen mode Exit fullscreen mode

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!" 
Enter fullscreen mode Exit fullscreen mode

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!

Top comments (0)