• Categories
    • Tutorials
    • Beginners Guide
    • WordPress News
    • WordPress Security
    • Best WordPress Plugins
    • WordPress Themes
    • Product Reviews
    • WP Tips & Tricks
  • Guides
    • Start a Blog
    • Make a Website
    • WordPress Hosting
  • WordPress Hosting
    • A2 Hosting
    • HostGator
    • Bluehost
    • Cloudways
  • Managed Hosting
    • WPEngine
    • Rocket.net
    • WPX
    • Kinsta
  • Coupons
    • WPEngine
    • Flywheel
    • Cloudways
    • A2 Hosting
    • WPX Hosting
WordPress Tutorials

How to Use WP-CLI for Better WordPress Site Management

Last Updated on: April 3, 2016 Rafay 1 Comment

How to Use WP-CLI for Better WordPress Site Management

The command line interface has always been popular in the world of developers, as it provides much more productivity and speed when developing a site.

WP-CLI helps web developers overcome the most head-scratching tasks in developing a WordPress website. In this article, we’ll walk you through a tutorial on how to setup WP-CLI and use its different commands to develop even faster and more efficient website.

Let’s get right to it!

Some Preliminary Steps

Before we move on to the tutorial, we need to make sure some things are in place for the command line interface for advanced WordPress management to install successfully.

Access to SSH

First of all, you need to make sure you have Secure Shell (SSH) access to your hosting service as most hosting services don’t provide SSH access by default. If you don’t have access, contact your hosting service’s customer support center for assistance.

Updated Software

After you’ve crossed SSH access off your list, you want to make sure you have WordPress 3.4 (or above) activated, PHP 5.3.2 (or above), and a UNIX-like environment. If you are a Windows user, you will need a command-line tool like Cygwin or a virtual machine setup. Once you have all the required resources to run WP-CLI, you can proceed to install it on your WordPress website.

Installing WP-CLI

Installing WP-CLI is quite simple. All you have to do is download a PHP file and put it somewhere on your drive so that you can run it from anywhere – which should be your main objective while setting up the command line interface. First, you’ll need to download the WP-CLI script from the GitHub repository to get the latest version available.

Once you’ve downloaded the .phar file, you need to make it an executable file. To do that, run the following command:

chmod +x wp-cli.phar

The last step for installation is that you need to move the newly created executable file to a folder so that we can run it from anywhere. Another handy dandy trick is to rename the executable file to something that’s easy to remember. That’s why the most commonly used name is wp. Once you’ve renamed the file, place it in the wp folder on your drive by using the following command:

sudo mv wp-cli.phar /usr/local/bin/wp

Congratulations! You have successfully installed WP-CLI and you can now run WP-CLI commands. Just to make sure that everything is in place you can run the following command:

wp –info

If everything went fine, you should be able to see this information displayed on the command line interface afterward:

PHP binary: /usr/bin/php
PHP version: 5.5.24
php.ini used: /etc/php.ini
WP-CLI root dir:  phar://wp-cli.phar
WP-CLI global config: /Users/kouratoras/.wp-cli/config.yml
WP-CLI project config:
WP-CLI version: 0.19.2

Now that we’ve successfully installed WP-CLI, let’s take a look at some of the commands you can run on it and the tasks they’ll accomplish.

Basic WP-CLI Commands and Tasks

Let’s start with the basics. You can easily install WordPress using WP-CLI’s core command. By using this command, you can make use of a set of powerful tools for managing WordPress installations.

Installing WordPress

First, we need to download WordPress in order to install it.  Navigate to the directory you’d like to download the WordPress CMS to and run the following command:

wp core download

This command will download the latest version of WordPress and after the download is complete, you can create the wp-config.php file using the core config command as follows:

wp core config --dbname=databasename --dbuser=databaseuser --dbpass=databasepassword --dbhost=localhost --dbprefix=prfx_

This command displays all of your login credentials which are usually present in the wp-config.php file. Now that the wp-config.php file is created, we can move on to installing WordPress. To install WordPress, you will have to execute this core install command:

[/php]wp core install –url=example.com  –title=”WordPress Website Title” –admin_user=admin_user –admin_password=admin_password –admin_email=admin@example.com[/php]

Check your credentials and make sure everything is in place. That’s all there is to it! You’ve installed WordPress on your server using WP-CLI.

Updating WordPress

Installing a clean, fresh copy of WordPress is one thing but updating WordPress is another. And just like installing WordPress, updating it is also very simple using WP-CLI. Using sub-commands, we are going to update our WordPress. The WP-CLI sub-commands enable users to update core files and database files if they aren’t updated. Here’s how:

wp core update
wp core update-db

But if you are not sure about your current version of WordPress then you can check it by using this command:

wp core version

Updating WordPress using the dashboard might seem much more efficient than it is through the command line but f you have several WordPress sites it can save you a lot of time and effort. By using WP-CLI, you can update multiple WordPress websites easily using this command:

#!/bin/bash
declare -a sites=('/var/www/wp1' '/var/www/wp2' '/var/www/wp3')
for site in "${sites[@]}";
do
wp --path=$site core update
done

Before any updating process, it’s always a good idea to backup your database.

Backup Database

WP-CLI has a one-liner WordPress backup command, the db export command:

wp db export backup.sql

It’s as simple as that! By running this command, you’ll have backed up your site’s database files in a jiffy.

Installing Themes

Installing themes with WP-CLI is very simple and less time consuming than going to the WordPress admin panel. WP-CLI connects your server directly to the WordPress theme repository and imports themes in just a few seconds. You can install any theme by using this command:

wp theme install themename

Just replace themename with the name of the theme you want to install.

Activating Themes

After installing the theme, you can activate it through WP-CLI using the following command:

wp theme activate themename

Activating themes through WP-CLI is easy to do and you’ll see the changes take effects in seconds.

Updating Themes

As you can probably already guess, updating WordPress themes with WP-CLI is just as easy as installing and activating them. The update command will save you a bunch of clicks and remembering how you’re supposed to update themes in the first place:

wp theme update themename

And if you want to update all of your themes while you’re at it, you can use this command instead:

wp theme update –all

This will help to update all of your themes in just a few seconds, unlike the traditional way of going to the admin panel and updating each theme individually.

Installing Plugins

Installing plugins with WP-CLI is an effortless task, as well. WP-CLI installs all of the plugins from the official WordPress plugin repository. You can use this command to install plugins:

wp plugin install pluginname

Simply replace pluginname with the name of the plugin you’d like to install to your site.

Activating Plugins

To activate the plugin, use the command:

wp plugin activate pluginname

That’s all there is to it! You’ve just installed a plugin to your WordPress using WP-CLI.

Deactivating Plugins

When you want to deactivate the plugin simply run the following command:

wp plugin deactivate pluginname

Updating Plugins

Similarly, updating plugins is a breeze. Whether you want to update a single plugin or all of the plugins you have installed, there’s a command for it with WP-CLI.

wp plugin update pluginname

Or if you’d like to update all of your plugins, just use this simple command:

wp plugin update –all

This command helps in updating all of your present plugins in seconds.

Managing Post Revisions

WordPress stores every revision of a post in your database which means that your site’s database contains a ton of information that you won’t ever have to use. Now that’s a really big issue. Unfortunately, WP-CLI does not manage post revisions with a simple command. That said, the wp-revisions-cli plugin, which is an extension of WP-CLI, helps in managing post revisions. This useful plugin can be found on the GitHub repository and you can install it just as you would install an ordinary WordPress plugin.

Bulk Import Images

Other management issues when administrating a WordPress site usually occur when handling media or images. Using WP-CLI, you won’t have to worry about a thing when it comes to media. The media tool of WP-CLI enables users to import all of their images and media onto their site with the following command:

wp media import images_folder/*

This is much easier compared to the ordinary admin panel method.

Search and Replace Functionality

A common practice employed by most developers is that they build a site on a local or development server and after completing the site, they move it to another server. In this process, copying and moving the database files is simple and easy to do. The tricky part is when you have to replace the old URLs with the new ones in the database. This tricky part won’t be so tricky if you are using WP-CLI. Using the search and replace command you can easily replace URLs in the database entries.

wp search-replace 'www.old-site.com' 'www.new-site.com'

This search and replace command helps developers finish their site in less time than expected. A lot of the hard work is done by WP-CLI so you won’t have to worry about making tiny mistakes or forgetting to update the URL for a particular page.

Wrapping It Up

WP-CLI is a must-have for all WordPress developers who want to speed up the development process without compromising on accuracy. There are endless possibilities with WP-CLI and it’s wide-range of features provide a good result to its users.

Have you used WP-CLI? We’d love to hear about your experience with it so let us know in the comments section below!

+ Share
Disclosure

Rafay

Related Posts

Back to all articles
  • WP Activity Log Review

    WP Activity Log Review: How to Secure Your Site and Log Activity

  • How to Stress Test WordPress Using a Free Tool (Full Guide)

  • How to Increase the Memory Limit in WordPress

    How to Increase the Memory Limit in WordPress

Coupons

View more deals
  • LearnDash Coupon Code!
    40% off

    LearnDash Coupon

    Searching for a LearnDash coupon to save on one of the top W
    Get This Deal
  • WP Activity Log Coupon
    20% OFF

    WP Activity Log Coupon

    Website security encompasses a whole host of elements, tasks, and
    Get This Deal
  • Recipe Card Blocks Coupon
    15% OFF

    Recipe Card Blocks Coupon

    Running a cooking or food website can be fun (and tasty) – but
    Get This Deal
1 Comment Leave a Reply
  1. Kafi says

    April 19, 2016 at 6:30 pm

    i think its very useful and also attractive. sure will give a try and let u know. thanks

    Reply

Leave a Reply Cancel reply

Full Disclosure This post may contain affiliate links, meaning that if you click on one of the links and purchase an item, we may receive a commission (at no additional cost to you). All opinions are our own and we do not accept payments for positive reviews.

Our Newsletter

Get awesome content delivered straight to your inbox.

Thank you!

You have successfully joined our subscriber list.

.

THE BEST OF WPKUBE

Some of the best content we have published so far.

BEGINNER GUIDES & REVIEWS

18 Best Cheap WordPress Hosting Providers in 2023 (From $1.99)
210 Best WordPress Hosting Options for 2023 (Pros & Cons)
38 Best Managed WordPress Hosting Providers for 2023 Compared
45 Best WooCommerce Hosting Providers Compared in 2023 (All Budgets)
5Top 9 Landing Page Plugins for WordPress (2023)
69 Best List Building Plugins for WordPress In 2023
7How to Fix the 500 Internal Server Error on Your WordPress Website
8Thrive Themes Review: A Look At The Full Membership
9Beaver Builder Review: Is it The Best Page Builder Plugin for WordPress (2023)?
10OptimizePress Review: Create Landing Pages with Ease
11How to Make a Website: Complete Beginner’s Guide
12Top 22 Best Free Stock Photo Resources For Your Site
1317 of the Best Google Fonts for 2023 (And How to Use Them in WordPress)
14How to Start a Blog in 2022 (Step by Step Guide)
15How To Fix ‘503 Service Unavailable’ WordPress Error
1611 Best Contact Form Plugins for WordPress in 2023
17How to Add a Custom Logo to Your WordPress Site
18How to Fix Error Establishing a Database Connection in WordPress

WPX Hosting: 50% OFF

Save 50% on WPX Hosting using our exclusive coupon code.

Get this Deal

Flywheel(our review)

Featured In Forbes Huffpost Entrepreneur SEJ

About WPKube

WPKube is an online WordPress resource which focuses on WordPress tutorials, How-to’s, guides, plugins, news, and more. We aim to provide the most comprehensive beginner’s guides to anything about WordPress — from installing plugins, themes, automated installs and setups, to creating and setting up pages for your website.

We have over 500+ tutorials, guides, product reviews, tips, and tricks about WordPress. Founded by Devesh Sharma, the main goal of this site is to provide useful information on anything and everything WordPress.

Twitter Facebook

Useful Links

  • Behind the Scenes
  • Beginner Guides
  • WordPress Hosting
  • WooCommerce Themes
  • MeridianThemes
  • Exclusive WordPress Deals
View All Guides »

Reviews

  • WPEngine 33% OFF
  • Thrive Leads
  • Flywheel 33% OFF
  • Divi Theme 20% OFF
  • Thrive Architect
  • Elegant Themes
Reviews »

Deals

  • InMotion Hosting
  • LifterLMS Coupon
  • LiquidWeb Coupon
  • WPEngine Coupon
  • A2 Hosting
  • FloThemes
More Deals »
© Copyright 2023 WPKube ® All Rights Reserved.
  • Contact
  • Site Terms
  • Disclosure
  • Privacy Policy