View Categories

Actions and filters of ShortPixel Image Optimizer

ShortPixel Image Optimiser calls the following actions and filters.

Increase WebP drop margin #

ShortPixel has a 5% margin between a JPG/PNG file and the resulting WebP/AVIF versions of the same file. In other words, if the resulting WebP/AVIF is more than 5% larger than the compressed JPG/PNG, it will be dropped by our plugin and not saved. This percentage can be adjusted with this filter:

add_filter('shortpixel/api/filesizeMargin', function () { return 30; });

Instead of 30, you can return any other number, even 200 (which means that WebP/AVIF will be generated even if it is twice as large).

Successful optimization #

This action is called upon successful optimization.

do_action( 'shortpixel_image_optimised', $post_id );

Pre-restore #

This action is called before restoring an image from backup.

do_action("shortpixel_before_restore_image", $post_id);

Post-restore #

This action is called after a successful restore.

do_action("shortpixel_after_restore_image", $post_id);

Backup folder #

v5.0 and newer #

Just before returning the ShortPixel backup folder (usually  /wp-content/uploads/ShortpixelBackups ):

$directory = apply_filters("shortpixel/file/backup_folder", $directory, $file);

v4.22.10 and older #

Just before returning the ShortPixel backup folder (usually /wp-content/uploads/ShortpixelBackups ), where the  $sizes  are the sizes array from metadata:

apply_filters("shortpixel_backup_folder", $backup_folder, $main_file_path, $sizes);

Post ID unset #

v5.0 and newer #

Post ID is not always set, only if it’s an image from Media Library:

apply_filters('shortpixel/file/exists', file_exists($path), $path, $post_id);

v4.22.10 and older #

Post ID is not always set, only if it’s an image from Media Library:

apply_filters('shortpixel_image_exists', file_exists($path), $path, $post_id);

URL filter #

This filters the URLs that will be sent to optimisation,  $URLs  is a plain array:

apply_filters('shortpixel_image_urls', $URLs, $post_id);

Size of queries #

Deprecated in v5.0 and newer

The $chunk  parameter is the value ShortPixel chooses to use as number of selected records in one query (based on total table size). Some hosts work better with a different value.

apply_filters('shortpixel/db/chunk_size', $chunk);

Parameters sent to API #

This filters the parameters sent to the optimization API (via  $requestParameters ), described in detail here: ShortPixel Reducer API$item_id  contains the ID of the Media Library item or the ID of the Custom Media item (if used). In short, this filter can be used to change all parameters sent to the API, as needed. For example, you can set different resize parameters for different post types, different compression levels, remove EXIF or not, convert to WebP/AVIF and basically any other parameter sent to the API for a given image (along with all its thumbnails).

apply_filters('shortpixel/api/request', $requestParameters, $item_id);

Paths of images sent for backup #

This filters the array of paths of the images sent for backup and can be used to exclude certain paths/images/thumbnails from being backed up, based on the image path. $mainPath  is the path of the main image, while $PATHs  is an array with all the files to be backed up (including thumbnails).

apply_filters('shortpixel/backup/paths', $PATHs, $mainPath);

Ignore DB information when WebP/AVIF files have been deleted from disk #

This filter is very useful in case some WebP/AVIF files have been deleted from the disk and ShortPixel is not aware of it. The plugin stores this information in the DB and therefore does not try to regenerate the missing files. If you use this filer the check of the DB is bypassed and the presence of the files is checked directly on the hard disk:

add_filter('shortpixel/image/filecheck', function () { return true; }); 

Exclude images from processing #

This filters the array ($sizes ) of image sizes which can be excluded from processing (displayed in the Advanced settings).

apply_filters('shortpixel/settings/image_sizes', $sizes);

Additional pages for background optimization #

Starting with version 5.6 and the Background mode feature, you may find this filter unnecessary.

This filter enables the background ShortPixel processing in additional pages (see here the original list). Here’s an example of this filter that enables the processing on the Comments screen (to be placed in your functions.php file):

add_filter('shortpixel/init/optimize_on_screens', function ($screens) {
	$screens[] = 'edit-comments';
	return $screens;
});

The edit-comments is the ID of the screen where you want to enable the processing.

If you want to add multiple pages, here’s how the snippet looks like:

add_filter('shortpixel/init/optimize_on_screens', function ($screens) {
	$screens = array('edit-comments', 'plugins', 'another-custom-post-type-page');
	return $screens;
	});

Make a backup of only the main image (not thumbnails) #

You can use the following filter to decide which images are skipped during the backup:

apply_filters('shortpixel/image/skip_backup', false, $this->getFullPath(), $this->is_main_file);

You should return true for the type of images that should be skipped during the backup. If you check if is_main_file is true and return false to this filter (do not skip backup), while otherwise returning true (for all other files), the backup will only be kept for the main image, which is what we want. The resulting code is then:

function sp_filter_thumbnails($result, $fullPath, $isMainFile ) {
        $result = true;
        if ($isMainFile) {
                $result = false;
        }
        return $result;
}
add_filter( 'shortpixel/image/skip_backup', 'sp_filter_thumbnails', 10, 3 );