Imagify Optimization & Filter Hooks
This reference guide covers the filter hooks available in Imagify to customize media optimization behavior. You can add these code snippets to a custom plugin or your active child theme’s functions.php file.
Prefer a plugin solution? You can use the Imagify Skipper helper plugin to configure exclusions directly from a simple settings page without touching code. Simply install the plugin and select which file formats (e.g., PDF, GIF) or paths to skip.
1. imagify_get_mime_types
Modifies the array of MIME types supported by Imagify. Unsetting a MIME type here completely removes support for that format plugin-wide across all Imagify workflows (including Media Library eligibility displays, statistics, and optimization runs).
Important Notes:
- Unsetting a MIME type will not un-optimize or restore files of that type that were already optimized in the past.
- This filter runs on different media contexts (images, PDFs, or both). Always use
unset()to remove a specific format instead of returning a new fixed array.
Parameters
array $mimes— Array of supported extensions and MIME types.
Code Example
/**
* Prevent Imagify from supporting GIF files site-wide.
*/
add_filter( 'imagify_get_mime_types', function( $mimes ) {
// Remove specific format: 'gif', 'pdf', 'png', 'jpg'
unset( $mimes['gif'] );
return $mimes;
} );
2. imagify_auto_optimize_attachment
Triggers during new media uploads in WordPress. Controls whether Imagify automatically optimizes the attachment upon upload.
Parameters
bool $optimize— Whether to optimize the attachment. Default:true.int $attachment_id— The WordPress attachment ID.array $metadata— Attachment metadata array.
Code Example
/**
* Skip automatic optimization on upload for specific MIME types.
*/
add_filter( 'imagify_auto_optimize_attachment', function( $optimize, $attachment_id, $metadata ) {
// Replace 'image/gif' with target MIME type (e.g., 'image/png', 'application/pdf')
if ( 'image/gif' === get_post_mime_type( $attachment_id ) ) {
return false;
}
return $optimize;
}, 10, 3 );
3. imagify_bulk_optimize_media
Triggers during Bulk Optimization runs. Allows excluding specific media items, paths, or formats from the bulk optimization queue without affecting auto-upload behavior.
Important Notes:
- This filter only applies to the bulk Optimization process. It does not affect Bulk Restore or “Generate Missing Next-Gen Images” runs.
- The total “X media to optimize” count displayed on the Bulk Optimization page is calculated separately, so excluded items will still be included in the displayed total count.
Parameters
bool $optimize— Whether to optimize this item. Default:true.int $media_id— The media item ID (attachment ID or custom folder file ID).string $context— Optimization context ('wp'or'custom-folders').int $optimization_level— Selected optimization level (0= Normal,1= Aggressive,2= Ultra).
Code Examples
A. Exclude Specific MIME Types from Bulk Run
/**
* Exclude PDFs from bulk optimization runs.
*/
add_filter( 'imagify_bulk_optimize_media', function( $optimize, $media_id, $context ) {
// Replace 'application/pdf' with target MIME type
if ( 'wp' === $context && 'application/pdf' === get_post_mime_type( $media_id ) ) {
return false;
}
return $optimize;
}, 10, 3 );
B. Exclude Media Uploaded Before a Specific Date
/**
* Exclude media items uploaded before a specific date during bulk runs.
*/
add_filter( 'imagify_bulk_optimize_media', function( $optimize, $media_id, $context ) {
if ( 'wp' === $context ) {
$cutoff_date = '2025-01-01';
$upload_date = get_the_date( 'Y-m-d', $media_id );
if ( $upload_date && $upload_date < $cutoff_date ) {
return false;
}
}
return $optimize;
}, 10, 3 );
C. Exclude Media by Specific Folder or File Path
/**
* Exclude media items stored in a specific folder path during bulk runs.
*/
add_filter( 'imagify_bulk_optimize_media', function( $optimize, $media_id, $context ) {
if ( 'wp' === $context ) {
$file_path = get_attached_file( $media_id );
// Skip media located inside a specific subfolder (e.g., /uploads/2019/ or /uploads/elementor/)
if ( $file_path && false !== strpos( $file_path, '/uploads/2019/' ) ) {
return false;
}
}
return $optimize;
}, 10, 3 );