Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save alexmoise/201a3b1196facb8343e82d0590f9b1ab to your computer and use it in GitHub Desktop.
Save alexmoise/201a3b1196facb8343e82d0590f9b1ab to your computer and use it in GitHub Desktop.
A simple plugin that makes it possible to upload USDZ files in WordPress Media Manager
<?php
/**
* Plugin Name: Allow USDZ mime in Wordpress Media Library
* Plugin URI: https://gist.github.com/alexmoise/201a3b1196facb8343e82d0590f9b1ab
* Description: A simple plugin that makes it possible to upload USDZ files in Media Manager. Don't forget to add the MIME type in web server config if necessary! No settings necessary, just activate and upload USDZ files in media library as usually. Tested with WP 5.2.3
* Version: 1.2.81
* Author: Alex Moise
* Author URI: https://moise.pro
*/
function mos_filter_fix_wp_check_filetype_and_ext( $data, $file, $filename, $mimes ) {
if ( ! empty( $data['ext'] ) && ! empty( $data['type'] ) ) {
return $data;
}
$registered_file_types = ['usdz' => 'model/vnd.usdz+zip|application/octet-stream|model/x-vnd.usdz+zip'];
$filetype = wp_check_filetype( $filename, $mimes );
if ( ! isset( $registered_file_types[ $filetype['ext'] ] ) ) {
return $data;
}
return [
'ext' => $filetype['ext'],
'type' => $filetype['type'],
'proper_filename' => $data['proper_filename'],
];
}
function mos_allow_usdz( $mime_types ) {
if ( ! in_array( 'usdz', $mime_types ) ) {
$mime_types['usdz'] = 'model/vnd.usdz+zip|application/octet-stream|model/x-vnd.usdz+zip';
}
return $mime_types;
}
add_filter( 'wp_check_filetype_and_ext', 'mos_filter_fix_wp_check_filetype_and_ext', 10, 4 );
add_filter( 'upload_mimes', 'mos_allow_usdz' );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment