WP-CLI - Do you already use the command line for WordPress?

WP-CLI - Do you already use the command line for WordPress?

Command line programs 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 program for WordPress: WP-CLI.

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

The platform has to face new challenges these days. This is partly due to the fact that the web is becoming more and 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 like the familiar command line. And not without reason - this great tool can also be used when using WordPress and can make many processes easier for you too!

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 program". You can quickly understand 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 program 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 part.

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:

WP-CLI - Do you already use the command line for WordPress?

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 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 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. 

"*" indicates required fields

I would like to subscribe to the newsletter to be informed about new blog articles, ebooks, features and news about WordPress. I can withdraw my consent at any time. Please note our Privacy Policy.
This field is used for validation and should not be changed.

What is WP-CLI and what is it used 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 eat 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:

wp-cli-wp-install

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 disk 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. 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 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 quite simple: 

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

wp-cli-local-1

Run the file to make sure it works: 

wp-cli-local-2

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.

How to move the file:

wp-cli-local4

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 would be very questionable 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.

wp-cli-alias1

2. move the file to the folder.

wp-cli-alias2

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

wp-cli-alias3

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.

wp-cli-alias4

Add these two lines and save the file.

wp-cli-alias6

Then log out of your shared hosting account and log back in so that the changes 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-test

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 <Befehl> 

"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: 

wp-cli-version

Configure WordPress 

wp config <Befehl>

Short note

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).

Before you create and configure your configuration file (the wp-config.php), you should fulfill 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 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.

WP-CLI - Do you already use the command line for WordPress?

Install WordPress

Again, replace the sample data with your own information.  

wp-cli-wp-install

Manage plugins and themes

wp plugin <Befehl>

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

wp-cli-plugins

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

wp-cli-dummy-data_1
WP-CLI - Do you already use the command line for WordPress?

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. 

wp-cli-db-backup

Search and replace

When you transfer a website in development to a live environment or import a new database into a live site, it often happens that you 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

wp-cli-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 familiarize yourself with WP-CLI, the more ways you'll find to improve the efficiency of your workflow. Have fun and happy experimenting! 

Your questions about WP-CLI

What questions do you have about WP-CLI? We look forward to your comment. Are you interested in current topics related to WordPress, web development and web design? Then follow us on Twitter, Facebook, LinkedIn or via our newsletter.

Did you like the article?

With your rating you help us to improve our content even further.

Write a comment

Your e-mail address will not be published. Required fields are marked with *