Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arunbasillal/39a4f60ad36266f49ded211df9512934 to your computer and use it in GitHub Desktop.
Save arunbasillal/39a4f60ad36266f49ded211df9512934 to your computer and use it in GitHub Desktop.
IAP: Remove trailing hypens from image filename
/**
* 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 );
@arunbasillal
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment