Filter and Hooks, Programming, WordPress

Remove special character while uploading Media

The media having special characters in its name,  may not be visible in the front-end, therefore, we should remove the special character while  uploading media in the WordPress dashboard.   

wp_handle_upload_prefilter, is admin filter that is called by the wp_handle_upload function, which helps to alter the filename before the file is moved to its destined location. The sole parameter $file, represent a single element of the $_FILES array.

add_filter('wp_handle_upload_prefilter', 'app_upload_filter',1,1 );
function app_upload_filter( $file ){
    $file['name'] = preg_replace('/[^a-zA-Z0-9-_\.]/','-', $file['name']);
    return $file;
}

Adding the aforementioned filter in the function.php, removes the special character from the name of the media prior to uploading it in WordPress dashboard.

Views: 52

Standard