{"id":844,"date":"2024-05-07T11:44:19","date_gmt":"2024-05-07T11:44:19","guid":{"rendered":"https:\/\/kb.shortpixel.com\/docs\/actions-and-filters-of-shortpixel-image-optimizer\/"},"modified":"2025-11-21T12:38:38","modified_gmt":"2025-11-21T12:38:38","password":"","slug":"actions-and-filters-of-shortpixel-image-optimizer","status":"publish","type":"docs","link":"https:\/\/kb.shortpixel.com\/knowledge-base\/article\/actions-and-filters-of-shortpixel-image-optimizer\/","title":{"rendered":"Actions and filters of ShortPixel Image Optimizer"},"content":{"rendered":"\n<p><a href=\"https:\/\/wordpress.org\/plugins\/shortpixel-image-optimiser\/\" target=\"_blank\" rel=\"noopener\">ShortPixel Image Optimiser<\/a> calls the following actions and filters.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Increase WebP drop margin<\/h2>\n\n\n\n<p>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:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">add_filter('shortpixel\/api\/filesizeMargin', function () { return 30; });<\/pre>\n\n\n\n<p>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).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Successful optimization<\/h2>\n\n\n\n<p>This action is called upon successful optimization.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">do_action( 'shortpixel_image_optimised', $post_id );<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Pre-restore<\/h2>\n\n\n\n<p>This action is called before restoring an image from backup.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">do_action(\"shortpixel_before_restore_image\", $post_id);<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Post-restore<\/h2>\n\n\n\n<p>This action is called after a successful restore.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">do_action(\"shortpixel_after_restore_image\", $post_id);<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Backup folder<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">v5.0 and newer<\/h3>\n\n\n\n<p>Just before returning the ShortPixel backup folder (usually&nbsp; <code class=\"inline-code\">\/wp-content\/uploads\/ShortpixelBackups<\/code> ):<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">$directory = apply_filters(\"shortpixel\/file\/backup_folder\", $directory, $file);<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">v4.22.10 and older<\/h3>\n\n\n\n<p>Just before returning the ShortPixel backup folder (usually <code class=\"inline-code\">\/wp-content\/uploads\/ShortpixelBackups<\/code> ), where the&nbsp; <code class=\"inline-code\">$sizes<\/code> &nbsp;are the sizes array from metadata:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">apply_filters(\"shortpixel_backup_folder\", $backup_folder, $main_file_path, $sizes);<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Post ID unset<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">v5.0 and newer<\/h3>\n\n\n\n<p>Post ID is not always set, only if it\u2019s an image from Media Library:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">apply_filters('shortpixel\/file\/exists', file_exists($path), $path, $post_id);<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">v4.22.10 and older<\/h3>\n\n\n\n<p>Post ID is not always set, only if it\u2019s an image from Media Library:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">apply_filters('shortpixel_image_exists', file_exists($path), $path, $post_id);<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">URL filter<\/h2>\n\n\n\n<p>This filters the URLs that will be sent to optimisation,&nbsp; <code class=\"inline-code\">$URLs<\/code> &nbsp;is a plain array:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">apply_filters('shortpixel_image_urls', $URLs, $post_id);<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Size of queries<\/h2>\n\n\n\n<section class=\"callout-yellow\">Deprecated in v5.0 and newer<\/section>\n\n\n\n<p>The&nbsp;<code class=\"inline-code\">$chunk<\/code> 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.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">apply_filters('shortpixel\/db\/chunk_size', $chunk);<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Parameters sent to API<\/h2>\n\n\n\n<p>This filters the parameters sent to the optimization API (via&nbsp; <code class=\"inline-code\">$requestParameters<\/code> ), described in detail here: <a href=\"https:\/\/shortpixel.com\/api-docs\" rel=\"noopener\">ShortPixel Reducer API<\/a>;&nbsp;<code class=\"inline-code\">$item_id<\/code> &nbsp;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).<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">apply_filters('shortpixel\/api\/request', $requestParameters, $item_id);<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Paths of images sent for backup<\/h2>\n\n\n\n<p>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.&nbsp;<code class=\"inline-code\">$mainPath<\/code> &nbsp;is the path of the main image, while&nbsp;<code class=\"inline-code\">$PATHs<\/code> &nbsp;is an array with all the files to be backed up (including thumbnails).<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">apply_filters('shortpixel\/backup\/paths', $PATHs, $mainPath);<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Ignore DB information when WebP\/AVIF files have been deleted from disk<\/h2>\n\n\n\n<p>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:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">add_filter('shortpixel\/image\/filecheck', function () { return true; }); <\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Exclude images from processing<\/h2>\n\n\n\n<p>This filters the array (<code class=\"inline-code\">$sizes<\/code> ) of image sizes which can be excluded from processing (displayed in the Advanced settings).<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">apply_filters('shortpixel\/settings\/image_sizes', $sizes);<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Additional pages for background optimization<\/h2>\n\n\n\n<p class=\"has-luminous-vivid-amber-background-color has-background\">Starting with version 5.6 and the&nbsp;<a href=\"https:\/\/shortpixel.com\/knowledge-base\/article\/584-background-processing-using-cron-jobs-in-shortpixel-image-optimizer\" rel=\"noopener\">Background mode<\/a>&nbsp;feature, you may find this filter unnecessary.<\/p>\n\n\n\n<p>This filter enables the background ShortPixel processing in additional pages (see <a href=\"https:\/\/shortpixel.com\/knowledge-base\/article\/334-on-what-pages-does-spio-optimize-images\" rel=\"noopener\">here<\/a> the original list). Here&#8217;s an example of this filter that enables the processing on the Comments screen (to be placed in your functions.php file):<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">add_filter('shortpixel\/init\/optimize_on_screens', function ($screens) {<br>\t$screens[] = 'edit-comments';<br>\treturn $screens;<br>});<\/pre>\n\n\n\n<p>The <code class=\"inline-code\">edit-comments<\/code> is the&nbsp;ID of the screen where you want to enable the processing.<\/p>\n\n\n\n<p>If you want to add multiple pages, here&#8217;s how the snippet looks like:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">add_filter('shortpixel\/init\/optimize_on_screens', function ($screens) {<br>\t$screens = array('edit-comments', 'plugins', 'another-custom-post-type-page');<br>\treturn $screens;<br>\t});<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Make a backup of only the main image (not thumbnails)<\/h2>\n\n\n\n<p>You can use the following filter to decide which images are skipped during the backup:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">apply_filters('shortpixel\/image\/skip_backup', false, $this-&gt;getFullPath(), $this-&gt;is_main_file);<\/pre>\n\n\n\n<p>You should return <code class=\"inline-code\">true<\/code> for the type of images that should be skipped during the backup. If you check if <code class=\"inline-code\">is_main_file<\/code> is <code class=\"inline-code\">true<\/code> and return <code class=\"inline-code\">false<\/code> to this filter (do not skip backup), while otherwise returning <code class=\"inline-code\">true<\/code> (for all other files), the backup will only be kept for the main image, which is what we want. The resulting code is then:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">function sp_filter_thumbnails($result, $fullPath, $isMainFile ) {<br>        $result = true;<br>        if ($isMainFile) {<br>                $result = false;<br>        }<br>        return $result;<br>}<br>add_filter( 'shortpixel\/image\/skip_backup', 'sp_filter_thumbnails', 10, 3 );<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Deactivate AI Features<\/h2>\n\n\n\n<p>You can use the following filter to disable the AI features of the plugin:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">add_filter('shortpixel\/settings\/no_ai', function() { return true; } );<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Deactivate the trailing period (.) at the end of the AI ALT text<\/h2>\n\n\n\n<p>You can use the following filter to remove the trailing period (.) from the AI-generated ALT descriptions produced by our plugin:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">add_filter('shortpixel\/ai\/check_period', function() { return false; } );<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Hide the FastPixel and Unlimited banners on the settings page<\/h2>\n\n\n\n<p>You can use the following filter to hide the FastPixel banner which shows up at the bottom of the Settings page:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">add_filter('shortpixel\/settings\/no_banner', function() { return true; } );<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Hide the ShortPixel compression info on image page<\/h2>\n\n\n\n<p>You can use the following filter to hide the ShortPixel compression details shown on the image page when clicking an image in the Media Library.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">add_filter('shortpixel\/js\/media\/hide_in_popups', '__return_true');<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","template":"","meta":{"footnotes":""},"doc_category":[37],"glossaries":[],"doc_tag":[],"class_list":["post-844","docs","type-docs","status-publish","hentry","doc_category-shortpixel-image-optimizer"],"blocksy_meta":[],"year_month":"2026-04","word_count":1040,"total_views":"1244","reactions":{"happy":"0","normal":"0","sad":"0"},"author_info":{"name":"admin","author_nicename":"admin_mdli53m5","author_url":"https:\/\/kb.shortpixel.com\/author\/admin_mdli53m5\/"},"doc_category_info":[{"term_name":"ShortPixel Image Optimizer","term_url":"https:\/\/kb.shortpixel.com\/knowledge-base\/category\/shortpixel-image-optimizer\/"}],"doc_tag_info":[],"knowledge_base_info":[],"knowledge_base_slug":[],"_links":{"self":[{"href":"https:\/\/kb.shortpixel.com\/wp-json\/wp\/v2\/docs\/844","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kb.shortpixel.com\/wp-json\/wp\/v2\/docs"}],"about":[{"href":"https:\/\/kb.shortpixel.com\/wp-json\/wp\/v2\/types\/docs"}],"author":[{"embeddable":true,"href":"https:\/\/kb.shortpixel.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kb.shortpixel.com\/wp-json\/wp\/v2\/comments?post=844"}],"version-history":[{"count":11,"href":"https:\/\/kb.shortpixel.com\/wp-json\/wp\/v2\/docs\/844\/revisions"}],"predecessor-version":[{"id":2816,"href":"https:\/\/kb.shortpixel.com\/wp-json\/wp\/v2\/docs\/844\/revisions\/2816"}],"wp:attachment":[{"href":"https:\/\/kb.shortpixel.com\/wp-json\/wp\/v2\/media?parent=844"}],"wp:term":[{"taxonomy":"doc_category","embeddable":true,"href":"https:\/\/kb.shortpixel.com\/wp-json\/wp\/v2\/doc_category?post=844"},{"taxonomy":"glossaries","embeddable":true,"href":"https:\/\/kb.shortpixel.com\/wp-json\/wp\/v2\/glossaries?post=844"},{"taxonomy":"doc_tag","embeddable":true,"href":"https:\/\/kb.shortpixel.com\/wp-json\/wp\/v2\/doc_tag?post=844"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}