How to optimize WordPress images with WP-CLI and MegaOptim?

Learn more about how to optimize images with MegaOptim and WP CLI in WordPress

Developer Tools
Darko Gjorgjijoski
by Darko Gjorgjijoski March 4, 2021

How to use WP-CLI?

In this article we are going to explain how to use MegaOptim Image Optimizer plugin and WP-CLI to optimize images from command-line.

WP-CLI is command-line tool that can help executing WordPress specific tasks from the command-line.

If you are WordPress developer, System Administrator or your site is built with WordPress you can benefit of MegaOptim to optimize images via command-line and WP-CLI.

To use WP-CLI you will need to have SSH access to your hosting account and WP-CLI installed. If you don’t have contact your hosting support to see if they can enable it for you.

1.) Login into SSH

Open terminal or your cmd shell and type:

ssh user@host

2.) Verify if WP-CLI is installed

Some of the hosting companies on the market already have it preinstalled on their servers.

Navigate to the directory where your WordPress site is residing using.

cd public_html/

Once you are in the WordPress public folder verify if WP-CLI is installed as follows:

wp

If you get “command not found” message the server is missing WP-CLI. Follow this guide to install it.

3.) Automation with WP-CLI

Various commands are available out of the box and you can do wonderful things using the WordPress command-line interface.

Some basic WP-CLI commands are as follows:

  • wp core update – To update WordPress
  • wp plugin activate plugin-slug – To activate specific plugin
  • wp plugin deactivate plugin-slug – To deactivate specific plugin
  • wp db export – To backup the database
  • wp db optimize – To optimize the database
  • wp db repair – Tto repair the database

In the following section we will dig into how to use WP CLI and MegaOptim 😇


How to use MegaOptim CLI commands

We are commited to make our plugin as usable as possible, especially for developers so we devloped a nice and useful WP-CLI commands for your convenience.

To list all MegaOptim commands type:

wp megaoptim

Most of the commands are explained below

1.) How to optimize your media library?

To optimize the all the images in your WordPress Media Library type:

 wp megaoptim bulk

To optimize the media library images uploaded by author ID 5 type:

wp megaoptim bulk --author=5

To optimize the media library images uploded from specific date until specific date type:

wp megaoptim bulk --date_from=2019-10-01 --date_to=2019-11-01

2.) How to optimize folder with images?

To optimize all the images that are in specific folder type:

wp megaoptim optimize /path/to/folder --recursive

The recursive parameter is if you want to optimize sub-folders as well.

3.) How to optimize image by path?

To optimize image in specific location type:

wp megaoptim optimize /path/to/folder/image.jpg

4.) How to optimize single media library image by ID?

To optimize single media library attachment/image with ID 42 type:

wp megaoptim optimize 42

To specify level you can pass the --level=ultra parameter or to re-optimize image you can pass --force.

Possible levels are: intelligent, ultra, lossless.

Both --level and --force parameters are optional.

5.) How to restore optimized media library attachment?

If you have backups enabled in the MegaOptim settings it is possible to restore images.

To restore attachment/image with ID 42 type

wp megaoaptim restore 42

Or if you want to restore all optimized attachment type

wp megaoptim restore all

6.) How to check my MegaOptim account balance?

wp megaoptim info

 

Conclusion

The command-line interface greatly reduces time and opens new paths for automation, especially for those that are skilled with shell scripting. There are lots of different use cases to cover with shell scripts and we hope the CLI interface we built into MegaOptim Image Optimizer will be useful to those that want to play with the command-line.

Happy Scripting!

Leave a reply