July 18, 2022

WP-CLI logo

Anyone managing multiple local WordPress websites using MAMP can attest to the amount of time and effort it takes to keep them all up to date using the WordPress admin interface. WordPress plugins, themes and the WordPress core itself are all continuously being developed resulting in an endless stream of updates that need to be applied to each website. This update regimen can be a time-consuming process as each website needs to be logged into individually before applying each type of update separately! Wouldn't it be nice if there was a more automated way to apply all those plugin, theme and core updates in one go? Well, there is and it is called WP-CLI, the command line interface for WordPress. In fact, WP-CLI can not only take care of these updates, but it can also perform a whole range of other website management tasks as well with little interaction required from the user.


Downloading WP-CLI onto a Mac to use with MAMP

The method of installing WP-CLI on a Mac has been mostly described elsewhere. However, how to set the application up so that it can be used with MAMP is a little less clear, so let's go through it.

First, decide where on your computer you want to install the WP-CLI application. The key objective here, since you will be accessing WP-CLI via MacOS Terminal, is that its location should be within your PATH so that it is accessible from anywhere on the command line. If you type:

echo $PATH

into MacOS Terminal, you will see a series of locations on your computer separated by colons (':'), usually something like this:


These locations are the places your system will look inside to find the appropriate application (in this case WP-CLI) to run a command that you have issued on the command line.

To get the WP-CLI application into your system's PATH, one of two common strategies are commonly used: 

Option 1:

Navigate (in MacOS Terminal) to one of the folders that are already in your PATH and then download (see how below) the WP-CLI application there. So, for instance, in the above example you would change the directory you are in to the /usr/local/bin folder by using the command:

cd /usr/local/bin

and then download the WP-CLI application there using:

curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar

Once WP-CLI has been downloaded, you then want to make it executable with the following command:

chmod +x wp-cli.phar

Option 2:

Alternatively, navigate to any other location on your system outside of your system's PATH. Then, as in option 1, download the WP-CLI application there and make it executable.

However, unlike option 1, once the download is complete, you then have to add that folder location to your PATH (described next).

Adding WP-CLI to your PATH in bash and zsh

If you choose option 1 above, then WP-CLI will already be in your PATH and ready to go. If, however, you choose option 2 and download the WP-CLI application into a folder that is not in your system's PATH, you now have to add that folder location to the $PATH variable. To do this, you have to add a line of code to the MacOS Terminal shell configuration file. Which shell configuration file (there are several!) you need to modify depends on the shell type that is running in Terminal. 

To find out which shell you are running in MacOS Terminal, type:

echo $0

Until relatively recently, Macs used the bash shell as their default shell when you opened a Terminal window, so the $PATH variable had to be modified in the .bash_profile file. However, more recent MacOS versions have started using the z-shell or zsh as their default shell, so you have to modify one of its shell configuration files instead. The obvious choice to use for zsh is the .zshrc file. This file can be modified using a command line text editor like nano, but also by using text processing software such as TextEdit in the Mac GUI, so let's do it the more user-friendly way.

To open the configuration file in TextEdit, use this command in Terminal:

open ~/.zshrc 

Once opened, add the following line to the top of the .zshrc file (replace the text contained within the < > signs and the < > signs themselves with the actual file path of the location of your WP-CLI application):

export PATH=<file path to the download location of the WP-CLI app>:$PATH

(eg. export PATH=/Applications/MAMP/bin:$PATH )

Save and close the file.

Simplifying the name of the WP-CLI application

Once the location of the WP-CLI program is in your system's PATH, you will want to simplify WP-CLI's name from 'wp-cli.phar' to 'wp' so that it can be called more easily on the command line. Using MacOS Terminal, navigate to the folder (if you are not already there) containing the WP-CLI application and type the following to rename the file to 'wp'.

mv wp-cli.phar wp

This name change will allow you to call the WP-CLI app using the simple 'wp' command.

Using the PHP version that MAMP is using

Finally, the correct version of PHP on your Mac needs to be used for WP-CLI to execute the commands within your MAMP instance. To do this, we need to know which PHP version your MAMP application is using by looking at the PHP setting on the MAMP configuration window. In the example below, you can see MAMP is using PHP version 7.4.12: 

Now that we know which PHP version MAMP is using, we need to find the PHP version's folder and add it too to the system's PATH. On a Mac, MAMP is installed in the Applications folder. Inside MAMP you will find a 'bin' folder which itself will contain a 'php' folder. Inside this 'php' folder, you should find folders for several different versions of PHP including the version that we are looking for. Finally, the actual php application will be inside another 'bin'-named folder within this PHP-version folder. A little confusing, I know! Anyway, the file path should be something like this (the PHP version number may vary): 


Therefore, you need to open the .zshrc file again:

open ~/.zshrc 

and add the following line as a new line to the top of the file (NB: replace the PHP version in the file path with the one your particular MAMP application is using):

export PATH=/Applications/MAMP/bin/php/php7.4.12/bin:$PATH

This will add the PHP version MAMP is currently using to the beginning of the existing PATH (Note that this is placed before :$PATH meaning that it is placed ahead of the current PATH contents). The system searches the folders of the PATH from left to right, so by putting it first, the first version of the PHP application that it comes across will be the version that the system uses. As a result, it will always run MAMP's copy of the PHP application even if there are other versions of the application in other folders of the PATH. 

Save and close the .zshrc file and restart the Terminal for all the shell configuration changes we have made to take effect.

Using WP-CLI with MAMP

Now you should be able to type in WP-CLI commands (starting with 'wp') into your command line from anywhere in the Terminal and have it respond without a 'command not found' error. However, the 'wp' commands will only actually do anything when you are within a WordPress folder. Therefore, first, open the MAMP application and wait for the web server to start so that the local website database application will be accessible. Then navigate to your htdocs folder in the MAMP application folder usually with:

cd /Applications/MAMP/htdocs

Once you are there, enter one of your WordPress root folders where each represents an individual website. From here, you can execute the WP-CLI commands that will interact with the WordPress application within that folder.

Interacting with WordPress from anywhere in Terminal

At this point, you should be able to use WP-CLI to control a WordPress application from the command line, but only after you have navigated to a WordPress root folder.  In order to interact with a MAMP-hosted website using WP-CLI from anywhere in your Terminal, you will need to define the path to that website's folder in WP-CLI's configuration file, config.yml. First, create the file with:

touch ~/.wp-cli/config.yml

then open the file using the nano command line text editor:

nano ~/.wp-cli/config.yml

Once open, define the path to any website root folders in MAMP and link them with simple aliases similar to those shown below: 

This will allow you to issue a 'wp' command on a local website using its alias from anywhere in the terminal, for example:

wp @WS1 plugin update --all

Updating multiple MAMP websites

Now that you have got WP-CLI working with one website at a time, it's time to automate the process of updating plugins, etc across all of your local websites hosted within MAMP.  To do this requires the use of Alias Groups.

To use Alias Groups, we group the individual aliases under a separate alias as shown below:

This will allow you to apply the WP-CLI command across multiple local websites using the alias group name, eg.

wp @allsites plugin update --all

which will run through each website one at a time and apply the 'wp' command.

WP-CLI and shell scripts

Finally, to really automate the process, we can use a simple shell script to issue multiple WP-CLI commands one after the other as shown in the example below:

In the example above, all the plugins on all sites will be updated first, followed by all the out-of-date themes. Finally, the WordPress application itself will be updated on any sites that are not running the latest version.

Understanding the .htaccess file
{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}