phpjsonwordpress

Allow .json upload file in WordPress


I want to upload .json files in my media library in WordPress. The reason for this is that I'm using lottie animations and the output is an animated .svg file. I just want to retrieve the file url from the backend to use it in the frontend. Unfortunately .json doesn't work to import even with the usage of the following code:

function cc_mime_types($mimes) {
    $mime_types = array(
        'svg'     => 'image/svg+xml',
        'json'     => 'application/json',
    );
    return $mimes;
   }
add_filter('upload_mimes', 'cc_mime_types');

define( 'ALLOW_UNFILTERED_UPLOADS', true );

Does anyone knows a solution for this kind of problem?

Best regards!


Solution

  • In your original code, you're declaring the new mime types in $mime_types but don't do anything with it, instead you're returning the original unchanged $mimes.


    As for your working solution, you could use only one function as follows:

    function cc_mime_types($mimes) {
        $mimes['json'] = 'application/json';
        $mimes['svg'] = 'image/svg+xml';
        return $mimes;
    }
    add_filter('upload_mimes', 'cc_mime_types');