Manage your WordPress website with WP-CLI

Updated on November 21, 2023
Manage your WordPress website with WP-CLI header image

Introduction

WordPress Command Line Interface (WP CLI) is a powerful terminal-based tool that lets you take control of your WordPress site functionality in a single session. As a popular Content Management System (CMS), WordPress is best used through its user-friendly web dashboard, but graphical interfaces are subject to in-browser challenges, and security threats addressed by the WP CLI tool.

Key advantages of using WP CLI to manage your WordPress site include:

  • Enhanced Productivity

    WP-CLI offers improved productivity and focus on task execution with direct commands. This is important for backend developers as all tasks run in the background, and frontend users such as editors access the changes in real time without delays.

  • Improved Administrator Account Security

    Through the WordPress graphical dashboard, administrative accounts are subject to security threats such as key logging, and data collection browser extensions. WP CLI offers greater control with access to user account management, password, and your website security controls through a secure shell connection.

  • Advanced Control

    WP-CLI allows you to manage your WordPress themes, plugins, and core features making it easy to troubleshoot the website in case of critical errors and failures on your website.

  • Task automation and Scheduling

    WP-CLI is compatible with system CronJobs which scheduling of tasks such as full site backups, and feature upgrades on the server.

This article explains how you can manage your WordPress website using WPCLI. You will perform common WordPress tasks such as managing user permissions, then, install and update features such as plugins, and themes on your website.

Prerequisites

Before you begin, be sure to:

Install WP-CLI

By default, WP-CLI is activate and available on WordPress servers deployed through the Vultr Marketplace. If unavailable on your server, install WP-CLI as described in the following steps.

  1. Verify the installed PHP Version on your server.

     $ php -v

    Output:

     PHP 8.1.21 (cli) (built: Jul 12 2023 23:03:54) (NTS)

    To use WP-CLI, the installed version must be PHP 7 and above.

  2. Download the latest WP-CLI release file.

     $ curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
  3. Verify that the downloaded file is valid.

     $ php wp-cli.phar --info
  4. Grant execute privileges to run the file on your server.

     $ sudo chmod +x wp-cli.phar
  5. Move the file to /usr/local/bin/wp to activate the wp system-wide command.

     $ sudo mv wp-cli.phar /usr/local/bin/wp
  6. Run the following command to verify that WP CLI is available.

     $ wp --info

    Output:

     OS: Linux 5.15.0-71-generic #78-Ubuntu SMP Tue Apr 18 09:00:29 UTC 2023 x86_64
     Shell: /bin/bash
     PHP binary: /usr/local/lsws/lsphp81/bin/php
     PHP version: 8.1.21
     php.ini used: /usr/local/lsws/lsphp81/etc/php/8.1/litespeed/php.ini
     MySQL binary: /usr/bin/mysql
  7. To update WP-CLI to the latest version, run the following command.

     $ wp cli update

WP CLI Commands

You can access WP-CLI commands by running the wp help command to view executable commands. Among the commonly used commands, you can use the following WP-CLI commands to use the tool effectively.

  • wp help: View available WP CLI commands.
  • wp core download: Download the latest WordPress core files.
  • wp db create: Create a new database.
  • wp plugin install: Install a plugin from the WordPress marketplace.
  • wp theme install: Install a WordPress theme.

To view more WP-CLI commands, run the help command as below.

$ wp help

Manage WordPress Using WP CLI

In this section, use WP CLI commands to manage your WordPress site. Update WordPress, manage core files, install and activate plugins, manage themes, and install WordPress files on a new domain as described in the steps below.

To correctly use WP-CLI, switch to your WordPress webroot directory. For example /var/www/html.

$ cd /var/www/html

Depending on your WordPress installation, make sure to use WP-CLI in your WordPress files directory, or define the path when running the command as below.

Update WordPress Using WP-CLI

When your WordPress version needs an update to the latest version, update WordPress using WP-CLI in the steps below.

To avoid any data loss:

  1. Verify your installed WordPress version.

     $ wp core version

    Output:

     6.2.2
  2. To update WordPress to the latest version, run the following command.

     $ wp core update
  3. Verify the updated WordPress version.

     $ wp core version

    Output:

     Success: WordPress is up to date.

Manage WordPress Core Files

WordPress core files consist of the most important files that allow WordPress to run on your server. These include wp-admin, wp-includes, index.php, and the wp-config.php file you can update as described below.

  1. Verify the integrity of WordPress core files.

     $ wp core verify-checksums
  2. In a fresh directory, download new WordPress Core files.

     $ wp core download 

    The above command downloads the full WordPress core files. It’s important to re-download the files in case your WordPress site crashes and requires new files.

  3. To generate a new wp-config.php file and establish a connection to your database, use the following syntax.

     $ wp config create --dbname=testing --dbuser=wp --dbpass=securepswd --locale=ro_RO

    For example, to generate the file with wp-db, example-user, and strong-password as your database details, run the following command.

     $ wp config create --dbname=wp-db --dbuser=example-user --dbpass=strong-password

    To establish a successful database connection, make sure the user exists in your MySQL database.

  4. Verify that a new wp-config.php is available in your WordPress root directory.

     $ ls /var/www/html
  5. Confirm that the database lines match your set database information.

     $ cat wp-config.php
  6. Using WPCLI, create the new database.

     $ wp db create

    The above command creates a new database on your MySQL database server. If the user in your wp-config.php file does not exist on the server, the command fails. Hence, WP-CLI helps you change the database, and use your user connection to access the database server.

Install and Activate WordPress Plugins

  1. To install a new WordPress plugin, for example wordfence, run the following command.

     $ wp plugin install wordfence

    Make sure you use the correct plugin name to correctly install it in WordPress. You can find all available plugins in the WordPress marketplace.

  2. Activate the plugin.

     $ wp plugin activate woocommerce
  3. To deactivate a WordPress plugin using WP-CLI, for example wordfence, run the following command.

     $ wp plugin deactivate woocommerce

    The above command deactivates the plugin, and you can further delete the plugin from your WordPress installation by running the following command:

     $ wp plugin delete woocommerce

Manage WordPress Themes

  1. View all installed themes on your WordPress site.

     $ wp theme list
  2. Search themes by name in the WordPress marketplace, use the following syntax.

     $ wp theme search <query>

    For example, search using the Twenty keyword, run the following command.

     $ wp theme search Twenty
  3. To install a new WordPress theme such as Twenty Twenty-Three, run the following command.

     $ wp theme install twentytwentythree

    The above command installs the Twenty Twenty-Three theme from the WordPress marketplace, make sure you find and use the correct theme name to successfully use it.

  4. Activate the theme.

     $ wp theme activate twentytwentythree
  5. To update all themes installed on your WordPress site, run the following command.

     $ wp theme update
  6. Deactivate a theme.

     $ wp theme deactivate twentytwentythree
  7. Delete a theme. For example, Twenty Twenty-Three.

     $ wp theme delete twentytwentythree
  8. Verify a theme's status on your WordPress website.

     $ wp theme status twentytwentythree

    Output:

     Theme twentytwentythree details:
     Name: Twenty Twenty-Three
     Status: Active
     Version: 1.1
     Author: the WordPress team

Manage WordPress Users Using WP-CLI

WordPress users have 5 permission levels, administrator, editor, author, contributor and subscriber. You can set and manage WordPress users using WP-CLI as described in the steps below.

  1. To create a new WordPress user, follow the syntax below.

     $ wp user create <username> <email> --role=<role>

    For example, to create a new user example-user with the email user@example.com, and editor privileges, run the following command.

     $ wp user create example-user user@example.com --role=editor
  2. Set the user password.

     $ wp user update example-user --user_pass=strong-password

    Replace, strong-password with your actual target password to set for the user.

  3. Verify that the new user is available.

     $ wp user get example-user
  4. To update the user permissions. For example, from editor to administrator, run the following command.

     $ wp user update example-user --role=administrator
  5. Update the user password.

     $ wp user update example-user --user_pass=strong-password
  6. To delete a WordPress user by username, run the following command.

     $ wp user delete example-user
  7. View all available WordPress users.

     $ wp user list

    The above command lists all users available on the WordPress website.

Backup WordPress Using WP-CLI

To back up your WordPress website using WP-CLI, back up the site database, and use compression tools such as tar or zip to compress a copy of your web root directory as described below.

  1. Back up your WordPress database to a safe destination such as your home directory.

     $ wp db export ~/wp-backup.sql

    Output:

     Success: Exported to '/home/hum/backup.sql'.
  2. Using tar, create a new compressed copy of your WordPress webroot directory.

     $ sudo tar -czf ~/wordpress_backup.tar.gz /var/www/html/

    The above command creates a new compressed tar.gz copy of your WordPress files directory. Or, manually copy files without compression using the cp command as below.

     $ cp -r /var/www/html ~/WordPress-files
  3. When complete, verify that the new files are available in your destination directory. For this article, your user home directory.

     $ ls ~/

When making changes on your WordPress site, permalinks may change, and some URLs may incorrectly display errors when loaded. To update your WordPress permalinks, follow the steps below.

Update your WordPress permalink structure using the syntax below.

$ wp rewrite structure <permalink>

The WordPress Permalink structure consists of:

  • Plain
  • Post name
  • Numeric
  • Day and name
  • Month and name

Depending on your desired URL structure, your set structure works in the format: https://example.com/<permalink>

  1. For example, to set your new permalinks to post name, run the following command.

     $ wp rewrite structure /%postname%/
  2. Refresh the WordPress rewrite rules to save changes.

     $ wp rewrite flush
  3. To update your WordPress permalinks per URL, follow the syntax below.

     $ wp search-replace <‘old’> <‘new’>

    To replace the old URL https://example.com/2023/about-us.php with https://example.com/about-us.php, run the following command.

     $ wp search-replace https://example.com/2023/about-us.php https://example.com/about-us.php

    The above command finds the old URL and replaces it with the new URL.

  4. Visit your WordPress website to verify changes in the URL structure.

     https://example.com

Install WordPress on a fresh Domain Using WP-CLI

To install WordPress on a new domain or subdomain, make changes to your web server configurations, create a new MySQL database user, then, use WP-CLI to install WordPress as described below.

  1. Switch to the new domain’s web root directory. For example /var/www/wp-example.

     $ cd /var/www/wp-example/
  2. Using WP-CLI, download the WordPress core files.

     $ wp core download
  3. Create the wp-config file.

     $ wp config create --dbname=wp-db --dbuser=example-user --dbpass=strong-password
  4. Install WordPress using your wp-config details.

     $ wp core install --url=wp.example.com --title="Site-Title" --admin_user=admin-user --admin_password=strong-password --admin_email=user@example.com

    Replace the URL wp.example.com, website name Site-Title, administrator username admin, password with a strong administrator password, and user@example.com with your actual details to save in your WordPress website configuration.

  5. In a web browser, visit your domain name and verify that your WordPress website is available.

     https://wp.example.com

Conclusion

WP-CLI is a powerful WordPress management tool that lets you take control of your website backend. It’s an important tool for developers and site administrators to effectively perform several functions on the WordPress website. For more information about the tool, visit the WP-CLI documentation.

Next Steps

WordPress is a highly extensible Content Management System (CMS). To apply more configurations and fine-tune your website, visit the following resources.