Last active
November 7, 2023 12:10
-
-
Save arunbasillal/39a4f60ad36266f49ded211df9512934 to your computer and use it in GitHub Desktop.
IAP: Remove trailing hypens from image filename
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
/** | |
* Removes trailing hyphen from filename if any. | |
*/ | |
function prefix_iaff_clean_filename_helper( $file ) { | |
// Process only images. | |
$image_extensions = array ( | |
'image/jpeg', 'image/gif', 'image/png', 'image/bmp', 'image/tiff', 'ico' | |
); | |
// Return if file is not an image file | |
if ( ! in_array($file['type'],$image_extensions) ) { | |
return $file; | |
} | |
$image_extension = pathinfo( $file['name'] ); | |
// Trim trailing hyphens in the image filename. | |
$image_extension['filename'] = trim( $image_extension['filename'], '-' ); | |
$file['name'] = $image_extension['filename'] . '.' . $image_extension['extension']; | |
return $file; | |
} | |
add_filter( 'wp_handle_upload_prefilter', 'prefix_iaff_clean_filename_helper', 20 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is now part of Image Attributes Pro 4.4 - https://github.com/arunbasillal/auto-image-attributes-pro/commit/eec717dfe23befe45e0dd21e627d28e0defc4a94