Disable Automatic PDF File Compression from the Imagify WordPress Plugin

- Code Snippets, WordPress

Do you use the popular Imagify plugin for WordPress? This is an excellent tool for compressing your media library files in bulk. One common issue that pops up from time to time when using this plugin relates to the automatic compression of PDF files that can often ruin the quality of the file. Currently there is no option within the plugin to disable this, but we’ve included a quick code snippet below to help solve this!

To disable automatic compression of PDF files, please add the below code to your theme’s functions.php file.

function no_auto_optimize_pdfs( $optimize, $attachment_id, $metadata ) {
if ( ! $optimize ) {
return false;
}

$mime_type = get_post_mime_type( $attachment_id );

return 'application/pdf' !== $mime_type;
}
add_filter( 'imagify_auto_optimize_attachment', __NAMESPACE__ . '\no_auto_optimize_pdfs', 10, 3 );