Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uploading .kml files to WordPress

Tags:

wordpress

kml

I'm trying to upload .kml files to WordPress. I had this working at one point but the latest WordPress update seems to have broken it.

I'm using this function

function my_myme_types($mime_types){
$mime_types['kml'] = 'application/vnd.google-earth.kml+xml'; //Adding kml extension
$mime_types['kmz'] = 'application/vnd.google-earth.kmz'; //Adding kmz files
return $mime_types;
}
add_filter('upload_mimes', 'my_myme_types', 1, 1);

I get this error when uploading

"Sorry, this file type is not permitted for security reasons."

like image 236
user3369825 Avatar asked Dec 08 '22 19:12

user3369825


2 Answers

For KML/KMZ files to be properly supported, you'll have to use text/xml and application/zip instead, because WordPress compares the declared MIME type to the 'real' detected MIME type (see function wp_check_filetype_and_ext in wp-includes/functions.php for more details)

function add_upload_mimes($mimes) {
  $mimes['kml'] = 'text/xml';
  $mimes['kmz'] = 'application/zip';
  return $mimes;
}
add_filter('upload_mimes', 'add_upload_mimes');

Update (2019-02-28) : kml is detected as text/xml and not application/xml, changing the code accordingly should resolve the issue described in the comments below.

like image 94
Pascal Martineau Avatar answered Jan 15 '23 10:01

Pascal Martineau


This may be related as of after the latest wordpress update:

Trying to upload xml file and getting the same error message, updating upload_mimes WordPress filter hook doesn't work as well as using some file uploading or mime type managing plugins which essentially using the same filter hook.

Solution: update wp-config.php and add in the following line

define( 'ALLOW_UNFILTERED_UPLOADS', true );

and then remove this line after uploading the files to avoid potential security risk

like image 38
BlueP Avatar answered Jan 15 '23 10:01

BlueP