Skip to content

Instantly share code, notes, and snippets.

@UmeshSingla
Created January 31, 2017 12:16
Show Gist options
  • Save UmeshSingla/e2ced49577e778f276f41b6a8ee4a513 to your computer and use it in GitHub Desktop.
Save UmeshSingla/e2ced49577e778f276f41b6a8ee4a513 to your computer and use it in GitHub Desktop.
WP Smush - Skip a directory or a list of directories from Smushing
<?php
//Allows to skip multiple directories, as specified in array
add_filter( 'wp_smush_image', 'filter_by_directory', '', 2 );
function filter_by_directory( $smush, $id ) {
$file_path = get_attached_file( $id );
//List of directories to be ignored
$dirs = array(
'uploads/2016/11',
'uploads/2017/01'
);
if( !empty( $file_path ) ) {
foreach($dirs as $path) {
if( strpos($file_path, $path ) !== false) {
return false;
}
}
}
return $smush;
}
//Allows to skip a single path while smushing image
add_filter( 'wp_smush_image', 'filter_by_directory', '', 2 );
function filter_by_directory( $smush, $id ) {
$file_path = get_attached_file( $id );
if( !empty( $file_path ) && false !== strpos( $file_path, 'uploads/2016/11' ) ) {
return false;
}
return $smush;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment