Command line programmes are useful for many applications and can save you a lot of work. Today I will introduce you to a very helpful command line programme for WordPress : WP-CLI.
WordPress has evolved since its beginnings in 2004. Originally, it was a blogging platform designed to "democratize publishing" and quickly became very popular. The vision of WordPress co-founder Matt Mullenweg was to make publishing content on the internet accessible to everyone, regardless of technical knowledge.
The platform has to face new challenges these days. This is partly because the web is becoming more technically sophisticated, but also because much of the website is based on outdated code. Many (and especially more experienced people) would also like features like the command line they are familiar with. And not without reason - this great tool can also be used when using WordPress and can also make many processes easier for you!
To understand what WP-CLI is, we start with the "CLI". In German, we would translate a CLI (Command Line Interface) as "command line programme". You can quickly understand what this is by pressing Win+R on Windows, typing "cmd" and waiting to see what happens. On a Mac, all you have to do is start the "Terminal" application. The Box with the cryptic lines that opens there usually seems rather daunting to inexperienced people - but with increasing technical knowledge it becomes more and more familiar.
A command line programme then not only creates a kind of "feel-good" atmosphere, but can save a lot of work! To become proficient at developing and programming systems, working with command line interfaces in any form is an important part.
CLIs have been used as long as computers have existed. The principle is quite simple: You write one or more commands into the interface and your computer executes them. For example, this command outputs the user name with which you are currently logged in to your PC:
However, this is not very intuitive and requires that you know what you want from your computer and how to get there. That's why Graphic User Interfaces (GUI) like Windows and Mac OS emerged to control the computer in a visually appealing way. These graphical user interfaces eventually became the standard way for people to interact with computers.
But the benefits of CLIs are considerable. The two most important are increased work efficiency and automation of repetitive tasks. Now that you know what a CLI is, let's take a closer look at WP-CLI .
"*" indicates required fields
What is WP-CLI and what does it do?
What is WP-CLI and why should you use it? In short, WP-CLI is a file that allows you to execute specific commands in the WordPress terminal. You can find a complete list of commands in the official WP-CLI documentation. There is also the possibility to create your own WP-CLI commands. You can read how this works in the "Commands Cookbook".
With WP-CLI you can automate processes that would otherwise consume your valuable time, starting with the most basic task - installing WordPress. The basic installation of WordPress is not a big challenge, but if you have a site in local development and now want to host the staging or production site online, you will need to reinstall WordPress and the necessary plugins.
As an alternative to manual installation, you could connect to your server via SSH, navigate to the correct folder and run the following in the CLI:
This is much faster than the conventional installation and manual modification of the database.
WP-CLI at Raidboxes
If you want to use WP-CLI only with Raidboxes, there is good news! Because it is already pre-installed when you create a new Box (i.e. a WordPress website) via the Raidboxes Dashboard . If you are wondering how to connect to Raidboxes via an SSH terminal and use WP-CLI , I recommend our article "The perfect WordPress development workflow with Git & SSH".
Requirements for WP-CLI
A UNIX-like environment (OS X, Linux, FreeBSD, Cygwin), there is limited support for a Windows environment. So if you are working on a Windows computer, you will probably want to install Linux. One way to do this without having to partition your hard drive and boot Linux separately is WSL (Windows Subsystem for Linux). Feel free to check out the official documentation.
The minimum requirement for WP-CLI is currently PHP 5.6 or higher. Please note that older PHP versions are no longer supported by WordPress for security reasons and may therefore no longer be compatible with newer WordPress versions. You can check all currently supported PHP versions on the official PHP website. At Raidboxes we currently support PHP 7.2, 7.3 and 7.4( we will supportPHP 8 when WordPress is officially compatible with it).
WordPress 3.7 or higher is required to use WP-CLI. Versions older than the latest WordPress version may have obsolete features.
Local installation of WP-CLI
If you have already worked with NPM or Composer, the installation is quite simple:
Download the file wp-cli.phar (you can use the command wget or curl):
Run the file to make sure it works:
That's it! You can now execute the command php wp-cli.phar to start WP-CLI. To save you typing next time, move this file to your local bin/wp folder instead. This way you can start WP-CLI by simply typing wp.
This is how you move the file:
Do you use shared hosting?
If you host your website on a shared hosting account, you may run into a problem here. Most shared hosting services have disabled the use of the "sudo" command. This is because it would be very questionable from a security point of view. The command ensures that you can execute commands as Super Admin (or usually "root").
In this case there is a workaround - you can create an alias for the WP-CLI file.
Create a folder for the WP-CLI file.
Move the file to the folder.
Make sure that you are in the root directory of your folder.
In the root directory you should have a .bashrc file (.zshrc for Mac). In this file you can create your own custom aliases. If you don't know what an alias is, you will benefit greatly from learning it now. Briefly described, they are shortcuts for commands that you can create. See the documentation here.
Edit your .bashrc file with nano.
Add these two lines and save the file.
Then log out of your shared hosting account and back in again for the changes to take effect.
Test and basic syntax
As with almost all terminal commands, there is a certain syntax to show that you are using the WordPress CLI. In this case it is wp followed by a command. To test whether a language is installed, you can call the version number as the first command.
wp -info should show you something like this:
WP-CLI Application examples
We will now look together at some useful things you can do with WP-CLI. You can always type wp help to see all the available commands.
A quick note at this point: Raidboxes does not have the function to install and reinstall the WordPress core for security reasons. However, this should not be a problem as your Box is already set up with WordPress and WP-CLI at Raidboxes. If you have any further questions, please do not hesitate to contact our support.
Download, configure and install WordPress
"wp core download" downloads the latest version of WordPress in the folder where the command is executed.
Downloading from WordPress is pretty easy, but if you want a specific version, you can add that very easily:
Before you create and configure your configuration file (the wp-config.php), you should fulfil the following requirements:
- you have created a database for WordPress
- Create a user for this database
- added the user to the database and given him the correct permissions for WordPress.
To create the configuration file, use the following line and replace -dbuser/name/pass with your own information. Enabling debugging is of course optional here.
Again, replace the sample data with your own information.
ManagePlugins and Themes
Plugin and Theme installations can also be easily managed with WP-CLI. The syntax, i.e. the structure of the command, follows the same pattern. As soon as you have internalized it, it is very easy to install, activate/deactivate, update or completely delete Plugins or Themes.
Create dummy data
It is quite helpful to see what the posts will look like on your development page. With these commands you can easily create dummy data.
And if you made a mistake and added an extra 0 by mistake, you can of course remove the data again.
Back up your database and import a new database
Before you make extensive changes, you should always make a backup.
Search and replace
When you transfer a website under development to a live environment or import a new database into a livesite, it often happens that you have to change content or configurations. Replacing http with https is often necessary, otherwise a mixed content error will occur. A perfect way to do this is to search and replace with WP-CLI.
Conclusion on WP-CLI
As with all productivity tools, the real benefit is in supporting your own workflow. The more you learn about WP-CLI, the more ways you will find to improve the efficiency of your workflow. Have fun and try it out!