Patrick Moravec avatar

Share on social media

WP-CLI – Are you already using the command line for WordPress?

Command line programmes are practical for many applications and can save you a lot of work. Today I’m going to introduce you to a very helpful command line programme for WordPress: WP-CLI.

WordPress has evolved since its beginnings in 2004. It was originally a blogging platform that was developed for the “democratisation of 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.

Nowadays, the platform has to face new challenges. This is partly due to the fact that the web is becoming ever more technically sophisticated, but also because the majority of websites are based on outdated code. Many (and especially more experienced people) would also like to have features such as the familiar command line. 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, let’s start with the “CLI”. In German, we would translate a CLI (Command Line Interface) as a “command line programme”. You can quickly familiarise yourself with what this is by pressing Win+R on Windows, entering “cmd” and waiting to see what happens. On Mac, all you have to do is start the “Terminal” application. The box with the cryptic lines that opens there is usually rather daunting for 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 also save a lot of work! To become good at developing and programming systems, working with command line interfaces in any form is an important component.

CLIs have been used for as long as computers have existed. The principle is very simple: you give one or more commands to 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 you to know what you want your computer to do and how. That’s why Graphic User Interfaces (GUI) such as Windows and Mac OS emerged, allowing the computer to be controlled 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 the increase in work efficiency and the automation of repetitive tasks. Now that you know what a CLI is, let’s take a closer look at WP-CLI.

What is WP-CLI and what is it for?

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. It is also possible 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 take up 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 required plugins.

As an alternative to manual installation, you could connect to your server via SSH, navigate to the correct folder and execute 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 only want to use WP-CLI with Raidboxes, there’s 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’re 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

Surroundings

A UNIX-like environment (OS X, Linux, FreeBSD, Cygwin), there is only 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). Have a look at the official documentation.

PHP version

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.

WordPress version

WordPress 3.7 or higher is required to use WP-CLI. Versions older than the latest WordPress version may have outdated functions.

Local installation of WP-CLI

If you have already worked with NPM or Composer, the installation is fairly simple:

Download the wp-cli.phar file (you can use the wget or curl command):

Run the file to make sure it works:

That’s it already! You can now execute the command php wp-cli.phar to start WP-CLI. To save yourself this 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 encounter a problem here. Most shared hosting services have disabled the use of the “sudo” command. This would be very dubious for security reasons. The command ensures that you can execute commands as Super Admin (or usually also “root”).

In this case, there is a workaround – you can create an alias for the WP-CLI file.

1. create a folder for the WP-CLI file.

2. move the file to the folder.

3. make sure that you are in the root directory of your folder.

You should have a .bashrc file (for Mac .zshrc) in the root directory. 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. Check out 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 log in again for the changes to take effect.

Test and basic syntax

As with almost all terminal commands, there is a specific syntax to show that you are using the WordPress CLI. In this case, it’s 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

Let’s take a look together at some useful things you can do with WP-CLI. You can always type wp help to see all available commands.

A quick note at this point: For security reasons, Raidboxes does not have the function for installing and reinstalling the WordPress core. 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 contact our support team.

Download, configure and install WordPress

wp core

“wp core download” downloads the latest version of WordPress in the folder in which the command is executed.

Downloading WordPress is pretty easy, but if you want a specific version, you can add that very easily:

Configure WordPress

wp config

Short hint

The command ‘wp core config’ creates the file wp-config.php, but is obsolete and has been renamed to ‘wp config creates’. However, it is still possible to use the old command (see notes on Gitlab).

Before you create and configure your configuration file (the wp-config.php), you should fulfil the following requirements:

  • have created a database for WordPress
  • Create a user for this database
  • have added the user to the database and given them the correct authorisations for WordPress.

To create the configuration file, use the following line and replace –dbuser/name/pass with your own details. Switching on debugging is of course optional.

Install WordPress

Again, replace the sample data with your own information.

Manage plugins and themes

wp plugin

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. Once you have internalised it, it is very easy to install, activate/deactivate, update or completely delete plugins or themes.

Create dummy data

It’s quite helpful to see how the posts will look on your development site. With these commands you can easily create dummy data.

wp post generate

And if you have made a mistake and accidentally added an additional 0, you can of course remove the data again.

Back up your database and import a new database

You should always create a backup before making extensive changes.

Search and replace

When you transfer a website in development to a live environment or import a new database into a live site, you often have to change content or configurations. Replacing http with https is often necessary here, as otherwise a mixed content error will occur. A perfect way to do this is to search and replace with WP-CLI.

wp search-replace

Conclusion on WP-CLI

As with all tools that increase productivity, the real benefit lies in supporting your own workflow. The more you familiarise yourself with WP-CLI, the more ways you’ll find to improve the efficiency of your workflow. Have fun and happy experimenting!

Patrick Moravec avatar

Leave a Reply

Your email address will not be published. Required fields are marked *