Home Website TutorialsWordPress How to install WP-CLI for WordPress administration

How to install WP-CLI for WordPress administration

by Thạch Phạm
Published: Last Updated on
A+A-
Reset

What is WP-CLI?

WP-CLI is a feature that was born and developed in 2011. With WP-CLI, you can easily manipulate and manage your WordPress website simply through the command line interface. With the usual method, when you need to install Plugins, Themes, or edit WordPress-related things, you need to access the WordPress admin and perform operations by clicking the mouse, this sometimes takes time and is somewhat cumbersome and time-consuming.

That’s why WP-CLI was developed to help you handle the above problems; even if we don’t need to access the WordPress admin, we can still manage our website quickly and save time.

So how to install and use WP-CLI?

On the Share Hosting service at AZDIGI, WP-CLI is pre-installed by default. You can use it directly from the Terminal window on the Hosting interface or use Host information to SSH through software like Putty, Xshell..etc.

So if you use Server/VPS services but do not have WP-CLI pre-installed, how to install and use it, then in this article, AZDIGI will guide you on how to install WP-CLI to your server in the most detail.

Implementation Guide

Step 1: Install WP-CLI

First, you need to SSH into your VPS as root, you can refer to how to SSH into VPS in the instructions below:

After successful SSH, you need to run the following commands in turn:

AZDIGI Tutorial
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
php wp-cli.phar --info
chmod +x wp-cli.phar
sudo mv wp-cli.phar /usr/local/bin/wp
    

Command annotation:

  1. Download the WP-CLI tool from the Github homepage.
  2. Confirm successful download.
  3. Authorize execution of wp-cli.phar fille.
  4. Move WP-CLI to a partition where all users can use the command and rename it from wp-cli.phar to wp

You need to change wp-cli.phar to wp because, in the process of using the command, you just need to type wp instead of wp-cli.phar to make it simpler and easier to remember.

[root@sv106 ~]# curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 5437k  100 5437k    0     0  4048k      0  0:00:01  0:00:01 --:--:-- 4051k
[root@sv106 ~]# php wp-cli.phar --info
OS:	Linux 3.10.0-1127.el7.x86_64 #1 SMP Tue Mar 31 23:36:51 UTC 2020 x86_64
Shell:	/bin/bash
PHP binary:	/usr/local/php72/bin/php72
PHP version:	7.2.33
php.ini used:	/usr/local/php72/lib/php.ini
WP-CLI root dir:	phar://wp-cli.phar/vendor/wp-cli/wp-cli
WP-CLI vendor dir:	phar://wp-cli.phar/vendor
WP_CLI phar path:	/root
WP-CLI packages dir:	
WP-CLI global config:	
WP-CLI project config:	
WP-CLI version:	2.4.0
[root@sv106 ~]# chmod +x wp-cli.phar
[root@sv106 ~]# sudo mv wp-cli.phar /usr/local/bin/wp

So that WP-CLI has been successfully installed, you can use WP-CLI to manage your WordPress website.

Step 2: How to use WP-CLI

Note: When you install WP-CLI on server/VPS servers and perform operations as root when using WP-CLI commands, you need to add the option –allow-root after the line command otherwise, you will get a warning as below. If you are using Share host services or do not have to operate from the root user, you do not need to add this option behind.

Error: YIKES! It looks like you're running this as root. You probably meant to run this as the user that your WordPress installation exists under.
If you REALLY mean to run this as root, we won't stop you, but just bear in mind that any code on this site will then have full control of your server, making it quite DANGEROUS.
If you'd like to continue as root, please run this again, adding this flag:  --allow-root
If you'd like to run it as the user that this site is under, you can run the following to become the respective user:
    sudo -u USER -i -- wp 

Now we will go to learn some basic commands of WP-CLI.

  • Command to check the WP-CLI version
AZDIGI Tutorial
wp --info 
    
[root@sv106 ~]# wp --info --allow-root
OS:	Linux 3.10.0-1127.el7.x86_64 #1 SMP Tue Mar 31 23:36:51 UTC 2020 x86_64
Shell:	/bin/bash
PHP binary:	/usr/local/php72/bin/php72
PHP version:	7.2.33
php.ini used:	/usr/local/php72/lib/php.ini
WP-CLI root dir:	phar://wp-cli.phar/vendor/wp-cli/wp-cli
WP-CLI vendor dir:	phar://wp-cli.phar/vendor
WP_CLI phar path:	/root
WP-CLI packages dir:	
WP-CLI global config:	
WP-CLI project config:	
WP-CLI version:	2.4.0
  • Command to check the list of supported features via WP-CLI
AZDIGI Tutorial
wp help
    

As shown below, you can see that WP-CLI supports a lot of features related to WordPress, to use any feature you just need to type the corresponding command: wp < Features to use >

NAME
  wp
DESCRIPTION
  Manage WordPress through the command-line.
SYNOPSIS
  wp 
SUBCOMMANDS
  cache                 Adds, removes, fetches, and flushes the WP Object Cache object.
  cap                   Adds, removes, and lists capabilities of a user role.
  cli                   Reviews current WP-CLI info, checks for updates, or views defined aliases.
  comment               Creates, updates, deletes, and moderates comments.
  config                Generates and reads the wp-config.php file.
  core                  Downloads, installs, updates, and manages a WordPress installation.
  cron                  Tests, runs, and deletes WP-Cron events; manages WP-Cron schedules.
  db                    Performs basic database operations using credentials stored in wp-config.php.
  embed                 Inspects oEmbed providers, clears embed cache, and more.
  eval                  Executes arbitrary PHP code.
  eval-file             Loads and executes a PHP file.
  export                Exports WordPress content to a WXR file.
  help                  Gets help on WP-CLI, or on a specific command.
  i18n                  Provides internationalization tools for WordPress projects.
  import                Imports content from a given WXR file.
  language              Installs, activates, and manages language packs.
  maintenance-mode      Activates, deactivates or checks the status of the maintenance mode of a site.
  media                 Imports files as attachments, regenerates thumbnails, or lists registered image sizes.
  menu                  Lists, creates, assigns, and deletes the active theme's navigation menus.
  network               Perform network-wide operations.
  option                Retrieves and sets site options, including plugin and WordPress settings.
  package               Lists, installs, and removes WP-CLI packages.
  plugin                Manages plugins, including installs, activations, and updates.
  post                  Manages posts, content, and meta.
  post-type             Retrieves details on the site's registered post types.
  rewrite               Lists or flushes the site's rewrite rules, updates the permalink structure.
  role                  Manages user roles, including creating new roles and resetting to defaults.
  scaffold              Generates code for post types, taxonomies, plugins, child themes, etc.
  search-replace        Searches/replaces strings in the database.
  server                Launches PHP's built-in web server for a specific WordPress installation.
  shell                 Opens an interactive PHP console for running and testing PHP code.
  sidebar               Lists registered sidebars.
  site                  Creates, deletes, empties, moderates, and lists one or more sites on a multisite installation.
  super-admin           Lists, adds, or removes super admin users on a multisite installation.
  taxonomy              Retrieves information about registered taxonomies.
  term                  Manages taxonomy terms and term meta, with create, delete, and list commands.
  theme                 Manages themes, including installs, activations, and updates.
  transient             Adds, gets, and deletes entries in the WordPress Transient Cache.
  user                  Manages users, along with their roles, capabilities, and meta.
  widget                Manages widgets, including adding and moving them within sidebars.

For example, I need to see the details of the commands related to the Plugin, first, press the Q key to exit the above interface and use the corresponding command as follows:

AZDIGI Tutorial
wp plugin
    

Here you will be listed the specific commands supported with the Plugin such as: active, deactivate, delete, uninstall Plugin..etc

[root@sv106 ~]# wp plugin --allow-root
usage: wp plugin activate [...] [--all] [--network]
   or: wp plugin deactivate [...] [--uninstall] [--all] [--network]
   or: wp plugin delete [...] [--all]
   or: wp plugin get  [--field=] [--fields=] [--format=]
   or: wp plugin install ... [--version=] [--force] [--activate] [--activate-network]
   or: wp plugin is-active 
   or: wp plugin is-installed 
   or: wp plugin list [--=] [--field=] [--fields=] [--format=]
   or: wp plugin path [] [--dir]
   or: wp plugin search  [--page=] [--per-page=] [--field=] [--fields=] [--format=]
   or: wp plugin status []
   or: wp plugin toggle ... [--network]
   or: wp plugin uninstall [...] [--deactivate] [--skip-delete] [--all]
   or: wp plugin update [...] [--all] [--exclude=] [--minor] [--patch] [--format=] [--version=] [--dry-run]
   or: wp plugin verify-checksums [...] [--all] [--strict] [--format=]

See 'wp help plugin ' for more information on a specific command.

So, AZDIGI has briefly discussed the working mechanism of WP-CLI with WordPress. In the next articles, AZDIGI will show you how to install a WordPress website with WP-CLI and use WP-CLI to manage Plugins, Themes…

Summary

Using WP-CLI will greatly assist you in managing your WordPress website, it helps to make your work faster with just a few simple command lines, you can still perform management of your website without having to access the website administration interface. If you find this article interesting and useful, please share it widely.

Wishing you success!

If you need assistance, you can contact support in the ways below:

Đánh giá

Tham gia nhóm hỗ trợ Server - Hosting

Tham gia nhóm Hỗ trợ Server - Hosting & WordPress để cùng nhau hỏi đáp và hỗ trợ các vấn đề về WordPress, tối ưu máy chủ/server.

Tham gia ngay

Bài viết cùng chuyên mục

AZDIGI – Không chỉ là đơn vị hàng đầu trong lĩnh vực Web Hosting và Máy chủ, chúng tôi mong muốn mang lại những kiến thức bổ ích nhất và luôn cập nhật thường xuyên cho cộng đồng người đam mê thiết kế website, công nghệ,…

Vui lòng không sao chép nội dung nếu chưa xin phép. Designed and Developed by PenciDesign