Optimizing with WP-CLI

WP-CLI usage

EWWW Image Optimizer features a WP-CLI extension that allows you to optimize your images "en masse". Using WP-CLI has the added advantage that the command line has no time limits, and (normally) no memory limits. This makes it easy to just get things done. So how do we use it?

In some cases, you will need to install WP-CLI first, if your web host doesn't provide it by default. When you download it, the file is usually named wp-cli.phar, and the most basic usage is to run it as-is:

php wp-cli.phar --info

However, if you have root access to your server, you can follow the wp-cli instructions to make the file executable, and to install it in your "path". If you follow the instructions at wp-cli.org, this means you can run it by simply typing 'wp --info' at the command line, instead of using php, like we did above. If your webhost has it pre-installed, you will typically run it the same way:

wp --info

You can literally name the wp-cli.phar file anything you want, like wapuu or ninjutsu. For our purposes, we'll stick with the first example where we assume you have downloaded wp-cli.phar into the "root" of your WordPress folder. If your wp-cli binary is named something else, just substitute it throughout.

We will also assume that you are running wp-cli from your WordPress root folder, but you can use the --path option to tell wp-cli where your WordPress installation lives. This will output the WordPress core version:

php wp-cli.phar --path=/home/user/www/ core version

Using EWWW IO via WP-CLI

Now that we are familiar with WP-CLI, let's move on to the EWWW IO command. WP-CLI uses commands and sub-commands for various actions, and in our case 'ewwwio' is the command and 'optimize' is the sub-command. If we run it with no options, we'll get some usage information:

php wp-cli-phar ewwwio optimize

usage: wp ewwwio optimize <library> [<delay>] [--force] [--webp-only] [--reset] [--noprompt]

That tells us that the <library> parameter is required, since EWWW IO can optimize the Media Library, theme folders, NextGEN, FlaGallery, etc. To find out more about these options, we can use the --help flag/option:

php wp-cli.phar ewwwio optimize --help

This will display detailed usage information, and you can use the arrows to scroll, and 'q' to quit/exit from the help screen. So let's break this down a bit more.

The first, required, parameter is the type of image that we want to optimize. We call this the library or 'gallery' parameter.
Valid values are 'all' which will optimize everything on your site, 'media' which will optimize everything that the normal bulk optimizer does: the Media Library, your theme folder, and anything configured in Folders to optimize. Using 'nextgen' will tell the plugin to process any Nextcellent or NextGEN 2.x galleries, and 'flagallery' will optimize all GRAND FlaGallery images.

All the other parameters are optional, and if you look at the list above, you'll note some of them have dashes, and some don't. This indicates how you should type those parameters. The parameters without dashes must be in the exact order listed, whereas the others can follow after that in any order.

For example, to optimize the media library with a 5 second delay between images, you would do this:

php wp-cli.phar ewwwio optimize media 5

The --force option is the same as the Force Re-optimize checkbox on the Bulk Optimizer. EWWW IO normally skips images it has optimized before, but the --force option tells it to re-optimize everything regardless of previous actions.

If you stop the WP-CLI process midway through, it will resume where it left off when you run it again. To make it start over with a fresh scan, use the --reset option. This is NOT the same as the --force option. The --force option will re-optimize everything, the --reset option simply tells the plugin to re-scan all your images.

Using cron on some webhosts, you can schedule WP-CLI commands to run at a certain time every hour, day, week, or month. Normally, EWWW IO will prompt you to continue after it has scanned your images. To avoid that, you need to use the --noprompt option, like so:

php wp-cli.phar ewwwio optimize media 5 --noprompt

More WP CLI Tools!

We have some handy tools under Tools->EWWW Image Optimizer, and most of these can also be run from the CLI.

Use this one to restore all images from the EWWW IO backups (cloud or local):

php wp-cli.phar ewwwio restore

When WordPress scales down an upload that was over 2560px wide, it keeps the original on disk. This tool lets you remove the originals, and keep only the "full size" 2560px version.

php wp-cli.phar ewwwio remove_originals

If you run PNG to JPG or other conversion operations on your images and don't have EWWW IO configured to delete the originals, you can use this tool to remove the pre-converted originals later on:

php wp-cli.phar ewwwio remove_converted_originals

You might find that having WebP copies takes up too much disk space, or you might be using something like our Easy IO CDN that does WebP conversion & delivery without using up your disk space. You can use the WebP cleanup tool to remove all the .webp copies from your local server:

php wp-cli.phar ewwwio remove_webp

Whether you are a seasoned pro, or just getting your feet wet, using WP-CLI can give you super-powers, so don't be afraid to jump right in! If you have any further questions, just use our contact form and we'll be happy to assist you. Happy Optimizing!

Still need help? Contact Us Contact Us