0

I have made a theme to allowing uploading of files like GLB format to be used in WEBGL 3D rendering in a post.

Here's the code for adding supported file type:

function my_myme_types( $mime_types ) {
   $mime_types['glb'] = 'application/octet-stream';
   return $mime_types;
};
add_filter( 'upload_mimes', 'my_myme_types');

It works for the purpose of the post, kind of. The file did get uploaded and can be loaded successfully.

However the file did not show up in media library. which means I cannot manage and delete the file in wordpress admin, thus the files got accumulated through time.

function save_custom_meta_data($id) {

    /* --- security verification --- */
    if(!wp_verify_nonce($_POST['wp_3d_attachment_nonce'], plugin_basename(__FILE__))) {
      return $id;
    } // end if

    if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
      return $id;
    } // end if

    if('page' == $_POST['post_type']) {
      if(!current_user_can('edit_page', $id)) {
        return $id;
      } // end if
    } else {
        if(!current_user_can('edit_page', $id)) {
            return $id;
        } // end if
    } // end if
    /* - end security verification - */

    // Make sure the file array isn't empty
    if(!empty($_FILES['wp_3d_attachment']['name'])) {

        // Setup the array of supported file types. In this case, it's just glb.
        $supported_types = array('application/octet-stream');

        // Get the file type of the upload
        $arr_file_type = wp_check_filetype(basename($_FILES['wp_3d_attachment']['name']));
        $uploaded_type = $arr_file_type['type'];

        // Check if the type is supported. If not, throw an error.
        if(in_array($uploaded_type, $supported_types)) {

            // Use the WordPress API to upload the file
            $upload = wp_upload_bits($_FILES['wp_3d_attachment']['name'], null, file_get_contents($_FILES['wp_3d_attachment']['tmp_name']));

            if(isset($upload['error']) && $upload['error'] != 0) {
                wp_die('There was an error uploading your file. The error is: ' . $upload['error']);
            } else {
                add_post_meta($id, 'wp_3d_attachment', $upload);
                update_post_meta($id, 'wp_3d_attachment', $upload);     
            } // end if/else

        } else {
            wp_die("The file type that you've uploaded is not a glb.");
        } // end if/else

    } // end if

} // end save_custom_meta_data
add_action('save_post', 'save_custom_meta_data');

I have to stress that the file does show in my posts, and they are uploaded to the server. They just somehow does not show up in Media Library thus no way to management them or delete them inside Wordpress Admin. Any way to fix this?

Your Answer

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

Browse other questions tagged or ask your own question.