JSON files are a great way of storing data, but WordPress blocks their upload by default. Here's how to allow JSON File Uploads In WordPress.
We ran into this issue when trying to integrate Lotties into our sites, as they come in .JSON format. For security reasons, WordPress will block them from upload. To fix this, we need to add a new MIMEType for JSON, excluding that filetype from the security rules. Once implemented, you can upload a JSON to your WordPress media library, just as you do with images. Self hosting reduces third party requests and speeds up your site.
Install this code using Advanced Scripts or Code Snippets (simply copy paste) and toggle it on. Then just drag and drop your JSON into the media library.
function cc_mime_types($mimes) {
$mimes['json'] = 'application/json';
$mimes['svg'] = 'image/svg+xml';
return $mimes;
}
add_filter('upload_mimes', 'cc_mime_types');
Code language: PHP (php)
Here's where that code's from: StackOverflow article. This actually allows you to upload both JSON and SVGs.
The is an actual security risk here, so turn it on to upload your files, then toggle it off until you need to add a new JSON to your WordPress site. Also, PLEASE sanitize and scan the files for any issues/malicious elements. JSON security. SVG security.
There's also a Plugin that we've used in the past, which allows you to toggle many different MIME (file) types on your site. If you need a bit more power, check it out:
Thanks a lot James! Didn't seem to work for me though, added your code via code snippets, and still not going through. Could it be on the server side? I'm hosted via vultr on Cloudways..
Also by the way, great commenting interface.. what plugin are you using for commenting?
WPDisquz. It's great!
May be Cloudways... Worst case, try this out: https://wordpress.org/plugins/file-upload-types/
Thanks for this, very helpful!
How to add this code? As a java script file? But not working for me
Thanks for the code. it help upload the file, even when I enable json file with Elementor it didn't work without the code