Skip to content

Instantly share code, notes, and snippets.

@wpmudev-sls
Last active May 31, 2023 13:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wpmudev-sls/f07745e142e6dcb3c902838c9e428bfb to your computer and use it in GitHub Desktop.
Save wpmudev-sls/f07745e142e6dcb3c902838c9e428bfb to your computer and use it in GitHub Desktop.
<?php
/**
* Plugin Name: Wpsmush - Exclude Specific Attachments From Smushing
* Plugin URI: https://premium.wpmudev.org/
* Description: This plugin will help to exclude specific attachments from optimizing
* Author: Abdoljabbar Bakhshande @ WPMUDEV
* Author URI: https://wpmudev.com/profile/abdoljabbarbakhshande
* License: GPLv2 or later
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Excludes an attachment from being smushed.
*
* @param array $smush The smush data.
* @param int $id The attachment ID.
* @return array
*/
function exclude_attachment_from_smushing( $smush, $id ) {
// Get the attachment IDs to skip smushing.
$attachment_ids = [ 1 ]; // Example: array( 2, 4, 5 );
// Check if the attachment ID is in the list of IDs to skip.
if ( ! in_array( $id, $attachment_ids, true ) ) {
return $smush;
}
// Update the attachment meta to indicate that it should be skipped from smushing.
update_post_meta( $id, 'wp-smush-ignore-bulk', 1 );
// Return false to indicate that the attachment should not be smushed.
return false;
}
/**
* Registers the filter.
*/
add_filter( 'wp_smush_image', 'exclude_attachment_from_smushing', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment