Created
January 31, 2017 12:16
-
-
Save UmeshSingla/e2ced49577e778f276f41b6a8ee4a513 to your computer and use it in GitHub Desktop.
WP Smush - Skip a directory or a list of directories from Smushing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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