We are going to cover the remaining EWWW Image Optimizer filters in this article, so buckle your seat-belts, and be sure your seat is in the upright position… Also, have a look over Part 1 if you haven’t already.

The filters we will cover are:

  • ewww_image_optimizer_count_optimized_queries
  • ewww_image_optimizer_schedule
  • ewww_image_optimizer_folder_restriction
  • ewww_image_optimizer_ignore_hidden_files
  • ewww_image_optimizer_hidpi_suffix

The first one is a bit obscure, but it determines how many records are queried from the wp_postmeta table when counting the images that need to be optimized for the Bulk Optimize page. This is the bit of text that says something like “39998 images in the Media Library have been selected (1515 unoptimized), with 85728 resizes (11752 unoptimized).” It can be a bit resource intensive, so we’ve dialed it back to 3000 records at a time. If your server can handle more (or not so much), it might be useful to modify this value; for speed in the first case, and stability in the latter case.

As before, I will give you an example for that in the form of a plugin. Just like Part 1, you can copy this code into a .php file, drop it into your plugins folder and activate it.

<?php
/*
 Plugin Name: EWWW Meta Query Limit
 Version: .1
*/

add_filter( 'ewww_image_optimizer_count_optimized_queries', 'ewww_make_it_faster' );
function ewww_make_it_faster( $limit ) {
   return 10000;
}

Short and sweet, change 10000 to whatever value you want (do NOT use commas or decimals/periods) and you are ready to use it on your own site.

Next up is one that will let you change the frequency of Scheduled and Deferred optimization, which is set to hourly by default. As I’ve said in other places, the time frequencies set by wp_cron are approximate, not exact, because the scheduled jobs can only be run when your site gets traffic. At any rate, hourly might be too often for you, so you can change it to ‘twicedaily’ or ‘daily’. Other plugins might offer even more time options, but those are the default intervals available. There are two different scheduled jobs available in EWWW, so you can change the time for one or both, the first example is for changing both (even if both are not turned on, you can still use this example):

<?php
/*
 Plugin Name: EWWW Cron Schedule
 Version: .1
*/

add_filter( 'ewww_image_optimizer_schedule', 'ewww_slow_down', 10, 2 );
function ewww_slow_down( $interval, $task ) {
   return 'daily';
}

If you wanted to just do one and not the other, you can test the value of $task and act accordingly. Scheduled optimization is ‘ewww_image_optimizer_auto’ and Deferred optimization is ‘ewww_image_optimizer_defer’.

function ewww_slow_down( $interval, $task ) {
   if ( $task === 'ewww_image_optimizer_auto' ) {
      return 'daily';
   }
   if ( $task === 'ewww_image_optimizer_defer' ) {
      return 'twicedaily';
   }
}

The next filter affects Scheduled optimization (as well as Scan & Optimize) when used in conjunction with the Folders to Optimize setting. By default, you cannot configure folders outside of the WordPress root, or the WordPress uploads folder. This lets you replace the WordPress uploads folder with any other folder you choose. Then you can use that folder or any of it’s subfolders in the Folders to Optimize. Of course, you still need to have permissions to modify files in those folders, so you have have each user restricted to a specific virtual host (and associated folder), you cannot use this to optimize all your vhost folders, unless the permissions allow you to do so.

<?php
/*
 Plugin Name: EWWW Permit Folder
 Version: .1
*/

add_filter( 'ewww_image_optimizer_folder_restriction', 'ewww_permit_folder' );
function ewww_permit_folder( $folder ) {
   return array( 'basedir' => '/var/www/mycustomfolder/', 'baseurl' => '');
}

Now you can add /var/www/mycustomfolder/awesome_images/ to your Folders to Optimize and make those awesome images even better by optimizing them.

This next filter also affects Schedule optimization and the Scan & Optimize, as it determines whether or not to scan for hidden files and folders

<?php
/*
 Plugin Name: EWWW Scan Hidden
 Version: .1
*/

add_filter( 'ewww_image_optimizer_ignore_hidden_files', 'ewww_scan_hidden' );
function ewww_scan_hidden( $ignore ) {
   return false;
}

That’s all there is to that one, any return value other than true or false is invalid. Be sure you pay attention to which one you use. True means that you DO want to ignore hidden files, whereas false will allow you to optimize hidden images on your server. On any UNIX-like system (Mac, Linux, BSD, etc), this is any file or folder beginning with a dot, like .git or .svn. Although .git and .svn are bad examples because those will not be scanned regardless, but you get the idea.

The last filter is for those of you using retina plugins and images. EWWW uses the default suffix from the WP Retina 2x plugin to detect retina images that need to be optimized: ‘@2x’. If you’ve configured a different suffix, or you are using a different plugin that uses some other suffix for the file, you can tell EWWW to look for that instead:

<?php
/*
 Plugin Name: EWWW Retina Suffix
 Version: .1
*/

add_filter( 'ewww_image_optimizer_hidpi_suffix', 'ewww_retina_suffix' );
function ewww_retina_suffix( $suffix ) {
   return 'x2dpi';
}

Change ‘x2dpi’ to whatever you are using, don’t forget the single quotes, and you are all set.

I’ve covered a lot of things in these five filters, but if you have any questions, feel free to send them our way.