Skip to content

Instantly share code, notes, and snippets.

@UmeshSingla
Last active April 18, 2017 22:16
Show Gist options
  • Save UmeshSingla/579849c6d0e6211bf48a82ec927808fe to your computer and use it in GitHub Desktop.
Save UmeshSingla/579849c6d0e6211bf48a82ec927808fe to your computer and use it in GitHub Desktop.
Allows to skip image files have particular search term in the filename
<?php
/**
* @package WP Smush
*/
/*
Plugin Name: Smush - Skip Landing Page Images
Plugin URI: https://wordpress.org/plugins/wp-smushit/
Description: This allows you to skip images from smushing with name containing a string e.g. `landing_page`
Author: Umesh Kumar
Version: 1.0
Author URI: https://profiles.wordpress.org/umeshsingla
*/
//You'd need to add `landing_page` or a similar term to all the images you'd like to skip. Make sure to update the code in here
//according to the modification you make.
add_filter( 'wp_smush_image', 'skip_landing_page_images', '', 2 );
if( !function_exists('skip_landing_page_images') ) {
/**
* Skip a file from smushing based on matching search pattern in file name
*
* @param $smush
* @param $id
*
* @return bool
*/
function skip_landing_page_images( $smush, $id ) {
//If we don't have attachment id
if ( empty( $id ) ) {
return $smush;
}
//Get the File name
$file_name = get_attached_file( $id );
if ( empty( $file_name ) ) {
return $smush;
}
//Check for a particular search term in the file name
if ( strpos( $file_name, 'landing_page' ) !== false ) {
return false;
}
return $smush;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment