WP-CLI is the official command line tool for managing WordPress pages. For many activities, it is a more convenient and faster alternative to the WordPress control panel.
The WP-CLI command line utility is accessed via the terminal by establishing an SSH connection to your virtual server. You can read more here: Establishing an SSH connection.
Here we highlight some useful WP-CLI commands.
All commands with explanations and examples can be seen on the WP-CLI webpage as well as on the command line.
To see all the commands, enter:
> wp
or
> wp help
WP-CLI commands are grouped into categories according to activities.
Every so-called main category has it’s own sub-commands and options.
To see the core commands, enter:
> wp core
To see an explanation of the command with examples, enter:
> wp help core update
WordPress configuration
View the WordPress version:
> wp core version
View the configuration:
> wp config list
Looking up only database data:
> wp config list DB_
View one specific configuration setting:
> wp config get DB_NAME
Change configuration setting:
> wp config set DB_NAME db_name
Viewing the status, activating and deactivating the maintenance mode:
> wp maintenance-mode status > wp maintenance-mode activate > wp maintenance-mode deactivate
Installing and updating WordPress
Download WordPress files, specifying the version and discard the default theme and plugins:
> wp core download --version=5.5.3 --skip-content
Install WordPress in 5 seconds (database data needs to be added to the wp-config.php file beforehand):
> wp core install --url=domain.tld --title='Uus sait' --admin_user=nimi --admin_email=nimi@domain.tld
Update WordPress to the version you need and then update the database:
> wp core update --version=5.5.1 > wp core update-db
WordPress users, themes and plugins
List of all WordPress users, each user having their own unique ID:
> wp user list
Adding a user account with WordPress admin privileges:
> wp user create username name@domain.tld --role=administrator
Delete user (account ID = 123).
N.B. If you delete a user whose posts are not reassigned to another user with the --reassign
option, their posts will also be deleted.
> wp user delete 123 > wp user delete 123 --reassign=1234
Change user password. It is recommended to import the password from the file as shown in command 2, so that it does not remain in the command line history file (user ID = 123):
> wp user update 123 --user_pass=new-and-long-password > wp user update 123 --prompt=user_pass < user_password.txt
List of all plugins, status, version number and whether there is an update available:
> wp plugin list
Updating plugins (updating a specific plugin and updating all):
> wp plugin update hello > wp plugin update --all
Deactivating a specific plugin may be a temporary solution to restore the website, if WordPress has become unavailable (a PHP error message is displayed) after a WordPress or plugin update. This can also be a solution if you can no longer access your WordPress admin page and it’s showing the name of a plugin.
Deactivating a plugin:
> wp plugin deactivate plugin-name
List of all themes, status, version number and whether there is an update available:
> wp theme list
Updating themes:
> wp theme update twentytwenty
Activities related to the WordPress database
Saving the database to a file. A quick and convenient way to back up your database (it’s recommended to back up the database before making any major changes to it):
> wp db export
Importing database from a file:
> wp db import dbname-2020-01-01-abcd1234.sql
Search the database for specific text:
> wp db search 'find this text'
Replacing data in a database.
If you need to see what the replacement would change and in which tables before modifying, add the option: --dry-run
to the command.
If the text to be searched or replaced contains spaces or special characters, then it must be surrounded by quotation marks:
> wp search-replace --report-changed-only --dry-run --skip-columns=guid 'search this' 'replace with this'
Open mysql command line (the information to connect to the database is taken from the wp-config.php file):
> wp db cli
Changing WordPress URL
You may need to change the WordPress URL when migrating WordPress from one server to another, from a subdomain to a main domain or vice versa.
The URL of a WordPress website is specified in the database table options, with the fields home
and siteurl
.
In addition, the domain name is usually found in other database tables and files.
View the URL of the current WordPress website:
> wp option get home > wp option get siteurl
Changing the URL of a WordPress website:
> wp option update home https://domain.tld
change siteurl
– URL to the directory where WordPress is installed.
siteurl
is the same as the home
URL if WordPress is installed in the domains root directory.
If WordPress is installed in a subdirectory, for example “blog”, then the siteurl
value would be https://domain.tld/blog.
> wp option update siteurl https://domain.tld
Replacing the old URL in the whole database (except in the table posts
in column guid
).
Before making any changes, it is recommended to check in which tables the change will be made. Once you have an overview of the tables, uncheck --dry-run
to make the change:
> wp search-replace --report-changed-only --dry-run --skip-columns=guid https://old.tld https://new.tld
If the old domain needs to be replaced by the new domain in the files as well, and a lot of changes need to be made, then the replacement can be done from the command line.
Before replacing an old domain name, it is necessary to know whether this can be done for all files or only for specific directories and file types.
Most of the time, there is no need to make replacements in buffer and log files. The buffer files can be deleted, either manually or via the WordPress admin panel.
Accordingly, you must prepare your replacement command.
Search for the old domain of all files and display the names of these files (if necessary, omit https:// in front of the domain name):
> grep -irl 'https://old.tld' ./
Search the old domain for files with specific file extensions (use the --include
option to adjust the list of file extensions to your needs):
> grep -irl --include \*.php --include \*.html --include \*.css 'https://old.tld' ./
Replace the old domain with the new domain in files with a specific file extension (In this example, PHP files):
> find ./ -name '*.php' -exec sed -i 's~https://old.tld~https://new.tld~g' {} \;
WordPress file checksums
Sometimes it is necessary to check that WordPress files have not been modified. This is often a sign of a compromised WordPress. This can be done with the following two commands:
Check WordPress core files:
> wp core verify-checksums
Check WordPress plugins files:
> wp plugin verify-checksums --all