Use WP-CLI for WordPress maintenance

Last Revised: October 2, 2021

Maintaining a WordPress site is not usually complex, but sometimes it can be complex to manage many sites on the same server. So if you are a System Administrator who likes the console, you are surely one of those who WP-CLI falls in love with for all the functionality it allows.

On the official site you have available a very simple installation manual, but that is not what I want to talk about, but the possibility that this system has to do maintenance and health tests of the system.

NOTE: This code is based on WP-CLI version 1.5.0

Once you have it installed and working, you can try the following… you will see that on many occasions tasks are performed that may seem repetitive, but they are basically to check that everything is fine and work more calmly.

Installing WP-CLI

The installation is quite simple.

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
mv wp-cli.phar /usr/local/bin/wp
wp --info

With this we download the software, verify that it is and configure it in a folder so that it is available from the entire system.

From here we can do some things.

NOTE: The commands below are an example of what can be done, but you have documentation of all the commands if you want to do more specific things.

Verify that we have WP-CLI installed

The execution as simple as viewing the software installation information.

wp --info

WP-CLI Software Update

Before we start working with WP-CLI, what we will do is check which version we are using.

wp cli version

And later we will check if there is any new version.

wp cli check-update

Which would lead us, if necessary, to update the WP-CLI.

wp cli update

Analysis

Before making any changes, we will analyze the status of the website. Although WP-CLI allows you to pass through a local folder parameter, in this case we will do everything within the installation itself that we are going to analyze.

cd /web/example.com/

Once we are inside the WordPress installation, we will make a first verification to verify that the kernel is not hacked or infected and that the files correspond to the official download.

wp core verify-checksums

In the same way, we will do the work for the plugins, yes, only those that are in the official repository.

wp plugin verify-checksums

The next thing we are going to do is check the configuration we have on the [wp-config.php]. With this visual review we can detect if there is any value that we may not know or that we have not included.

wp config get

Later we will do an analysis of the users. This analysis especially goes well when you have a reduced list of users, because if you give access to other users, it can be quite long and useless to do this review.

wp user list

In the same way, we will check that the prefix of the tables of the database is correct.

wp db prefix

And, once verified, we will make a list of all the tables we have, to verify that they are correct and that they have not appeared or disappeared.

wp db tables

Once here, another piece of information that we can analyze is the “rewrite” configurations (clean URLs) that are available.

wp rewrite list

And in case you want to make a soft drink and regenerate them, you can do it.

wp rewrite flush

WordPress CORE Update

The first thing we are going to see is what version of the kernel we have installed.

wp core version

And then, if there is a new version available to install.

wp core check-update

If we want to, we can make a simple update, or we can, alternatively, force a version to be installed or overwrite the current one.

wp core update
wp core update --force --version 1.2.3

Subsequently, if an update is made, we should run the system that also does the database update if necessary.

wp core update-db

WordPress PLUGINS Update

To begin with, we are going to make a list of all the plugins that we have installed.

wp plugin list

And once we see what we have, we will review those that require an update.

wp plugin update --dry-run --all

If we are sure that we want to update everything, we can do it without problem, or we can go plugin by plugin if it is, for example, something complex (WooCommerce type).

wp plugin update --all
wp plugin update slug-del-plugin

WordPress THEMES Update

The case of templates is very similar to that of plugins. The first thing we will do is list all the templates we have.

wp theme list

And then see which ones can be updated.

wp theme update --dry-run --all

Thus, in the same way, we can update all or one in particular.

wp theme update --all
wp theme update slug-de-la-plantilla

WordPress TRANSLATIONS Update

Let’s start by making a list of the languages available on the site.

wp language core list --status=installed

And let’s see which languages require some kind of update.

wp language core update --dry-run

And then let’s make the updates.

wp language core update

Database

After maintaining all the software elements, the next thing could be to do a database review, especially after all the previous updates.

The next thing will be to see how much the database occupies. This data is unusual to have but many shared accommodations have limitations in their size, so it will be important to control their growth.

wp db size

Finally, we will make an optimization of the database that helps unlock any type of element that has stuck, in addition to the size and resources of the tables.

wp db optimize

In the specific case of needing a specific revision of the database because we think it may be corrupt or have a problem, we have the following commands that analyze, optimize and repair the problems that may exist.

wp db check
wp db optimize
wp db repair

Cache maintenance

WordPress cache is a system that can sometimes feel like a black box, but very useful for its performance and security. That is why we must be attentive to its maintenance.

The first thing we will do is a cleaning of the transient (cache in database). In this case we will eliminate those that are not necessary (the expired ones)

wp transient delete --expired

And if for some reason we need it, we have available the option to completely empty the disk cache or memory.

wp cache flush

Backup management

Export and import of the database

wp db export --porcelain
wp db import wpdatabase.sql

And in the same way, Export and import of the contents of the site according to the WP Data format (WXR file).

wp export
wp import examplewp.xml --authors=create

Regenerate Thumbnails

Sometimes due to changes in template or image sizes, it is necessary to regenerate all the thumbnails on the site. In this case we can make a massive version, or simply one of those that are not created or available.

wp media regenerate --yes
wp media regenerate --yes --only-missing

About this document

This document is regulated by the EUPL v1.2 license, published in WP SysAdmin and created by Javier Casares. Please, if you use this content in your website, your presentation or any material you distribute, remember to mention this site or its author, and having to put the material you create under EUPL license.