0

I have several uploaded files whose links I have later discovered yield a "page not found" message error, because their name contain the hat character ("^"). Have I to delete and re-upload them after changing their name or is there an option that makes Wordpress to correctly manage their original name?

  • I would hardly call that a hat, more like a hand or wing! ^(o.o)^ - Does it include that character in the slug, what does an example slug look like for one of those pages? – Howdy_McGee Apr 1 '15 at 15:29
  • 1
    In general, all special characters should be avoided in file names. See this – Pieter Goosen Apr 1 '15 at 15:39
  • This may not have anything to do with WordPress, and WordPress-- even PHP-- may not be able to do anything about it. You are dealing with, potentially, server and filesystem level issues. Your best approach is to change to file names. – s_ha_dum Apr 1 '15 at 16:10
  • @Howdy_McGee if the filename is x^2.xls the slug is/wp-content/uploads/2013/02/x2.xls – Toc Apr 4 '15 at 15:36
  • Files have been uploaded by others, not mine. And they are many. Moreover, I wonder that WordPress allows to upload a file with a potentially problematic character in its name but it is not able to manage it correctly. – Toc Apr 4 '15 at 15:39
2

This is a filter I use to keep my clients from adding special characters to their filenames, should work for you - you'll have to reupload the file though.

/**
 /* Sanitize Uploaded Filenames
 /* @param array $file
 /* @return array $file
 */
function sanitize_file_uploads( $file ){
    $file['name'] = sanitize_file_name($file['name']);
    $file['name'] = preg_replace("/[^a-zA-Z0-9\_\-\.]/", "", $file['name']);
    $file['name'] = strtolower($file['name']);
    add_filter('sanitize_file_name', 'remove_accents');

    return $file;
}
add_filter('wp_handle_upload_prefilter', 'sanitize_file_uploads');

This function only allows numeric, numbers, underscores, dashes, and periods. It converts it to lowercase and also run it through the accents function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.