{"id":2996,"date":"2019-07-14T12:58:20","date_gmt":"2019-07-14T11:58:20","guid":{"rendered":"https:\/\/kudzia.eu\/b\/?p=2996"},"modified":"2019-07-14T12:58:43","modified_gmt":"2019-07-14T11:58:43","slug":"allowing-custom-file-extensions-to-be-uploaded-to-wordpress","status":"publish","type":"post","link":"https:\/\/kudzia.eu\/b\/2019\/07\/allowing-custom-file-extensions-to-be-uploaded-to-wordpress\/","title":{"rendered":"allowing custom file extensions to be uploaded to wordpress"},"content":{"rendered":"<p>WordPress restricts types of files you can upload. it not only checks the extension of what you&#8217;re adding to the media library but also its mime type. so you cannot fool it by uploading .exe renamed to .jpeg &#8211; if you try it, you&#8217;ll get an error saying: &#8220;Sorry, this file type is not permitted for security reasons&#8221;.<\/p>\n<p>to allow upload of custom file type to wordpress 5.2.2 i&#8217;ve created few lines long plugin and placed it under wp-content\/plugins\/allowUploadOfCustomFileType.php :<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;?php\r\n\/**\r\n * @package allowUploadOfCustomFileType\r\n * @version 1.0\r\n*\/\r\n \/*\r\nPlugin Name: allow upload of custom file type\r\nPlugin URI: http:\/\/kudzia.eu\r\nDescription: allow upload of .ext files\r\nAuthor: pawel kudzia\r\nVersion: 1.0\r\nAuthor URI: http:\/\/kudzia.eu\/\r\n*\/\r\n\r\n\/*\r\n\/\/ this is how i used to do it, it's no longer enough\r\nfunction my_myme_types($mime_types){\r\n mime_types&#x5B;'ext'] = 'application\/some.document';\r\n return $mime_types;\r\n}\r\nadd_filter('upload_mimes', 'my_myme_types', 1, 1);*\/\r\n\r\n\/\/ and that's how i handle it now,\r\n\/\/ based on https:\/\/wordpress.org\/support\/topic\/cannot-import-csv-donations\/\r\nadd_filter('wp_check_filetype_and_ext', function($values, $file, $filename, $mimes) {\r\n if ( preg_match( '\/\\.(ext)$\/i', $filename ) ) {\r\n  $values&#x5B;'ext']  = 'ext';\r\n  $values&#x5B;'type'] = 'application\/some.document';\r\n }\r\n return $values;\r\n}, PHP_INT_MAX, 4);\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>WordPress restricts types of files you can upload. it not only checks the extension of what you&#8217;re adding to the media library but also its mime type. so you cannot fool it by uploading .exe renamed to .jpeg &#8211; if you try it, you&#8217;ll get an error saying: &#8220;Sorry, this file type is not permitted [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17,51],"tags":[79],"class_list":["post-2996","post","type-post","status-publish","format-standard","hentry","category-tech","category-unimportant","tag-wordpress"],"_links":{"self":[{"href":"https:\/\/kudzia.eu\/b\/wp-json\/wp\/v2\/posts\/2996","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kudzia.eu\/b\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kudzia.eu\/b\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kudzia.eu\/b\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kudzia.eu\/b\/wp-json\/wp\/v2\/comments?post=2996"}],"version-history":[{"count":3,"href":"https:\/\/kudzia.eu\/b\/wp-json\/wp\/v2\/posts\/2996\/revisions"}],"predecessor-version":[{"id":2999,"href":"https:\/\/kudzia.eu\/b\/wp-json\/wp\/v2\/posts\/2996\/revisions\/2999"}],"wp:attachment":[{"href":"https:\/\/kudzia.eu\/b\/wp-json\/wp\/v2\/media?parent=2996"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kudzia.eu\/b\/wp-json\/wp\/v2\/categories?post=2996"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kudzia.eu\/b\/wp-json\/wp\/v2\/tags?post=2996"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}