Upload Flash Files in Media Library

I learned something new today and I thought I would share. I discovered that it was no longer possible to upload Shockwave Flash files in the WordPress Media Library. Not that I have to upload flash files very often but I had a client that wanted to add a flash game in their website and could not for the life of them get it uploaded.

This change was apparently added in WordPress 3.6.1.

Additional security hardening:

Updated security restrictions around file uploads to mitigate the potential for cross-site scripting. The extensions .swf and .exe are no longer allowed by default, and .htm and .html are only allowed if the user has the ability to use unfiltered HTML

While I can understand the reasoning behind it, I still had to find a way to allow my client to upload their swf file. Well a little searching and a little tweaking here and there and I came up with the following snippet of code. I added it to their theme functions file and presto they could upload their swf file and everyone was happy again.

[php] // Add SWF mime type to upload to media manager function lgr_mime_types($mime_types){ if (current_user_can(‘install_plugins’)) { $mime_types[‘swf’] = ‘application/x-shockwave-flash’; } return $mime_types; } add_filter(‘upload_mimes’, ‘lgr_mime_types’, 1, 1); [/php]

I could have done the code without the current_user_can check, but the clients site in question has multiple users and I only wanted people that had Administrator rights to be able to add swf files in case you were wondering what that check was for.

Seems to be working so far. Will want to move it into a site specific plugin in the future but for the next couple of days this will do nicely.

Categories: snippet