3 Ways Of Uploading JSON Files To WordPress

3 Ways Of Uploading JSON Files To WordPress

WordPress has multiple rules set up to secure your website for files. One of those rules disallows the upload of JSON files. I found out when I had to upload JSON files for my Lottie animations, and luckily there are multiple ways of solving this.

The most secure and simple way of allowing JSON uploads in your WordPress website is by installing the WordPress plugin WP Add Mime Types. After installing and activating the plugin you add the line json = application/json. And then you can upload JSON in your media library.

This is one of the ways, and if you want a lean way where you don’t have to add a plugin, then this is also possible by adding a short snippet of code. Don’t worry. It’s very straightforward, and I’ve made a video of how easy it is to do.

Method 1: Use WP Add Mime Types To Allow JSON Upload

This is probably the most straightforward way to allow JSON file upload if you don’t want to touch any code.

You start by installing the plugin WP Add Mime Types and activating it. After this then you add the line json = application/json in the textarea and press save. Now WordPress will allow you to upload JSON files in your media library like you were uploading an image.

If you experience an error when you’re trying to upload your JSON file, even after installing the plugin, it might be because of your folder permissions.

To fix this, I’ve written an article on how you fix your upload errors in WordPress. You can read it here.

Method 2: Allow JSON Upload By Adding Code Snippet To Functions.php

If you’re just a little bit handy with coding, or you wish to keep your amount of plugins installed to a minimum, then this is the solution for you.

[cboxarea id=”cbox-DZ7YzH84qT48YAf1″]

Start by heading over to Appearance -> Theme File Editor. On the right side, you’ll want to click on the Theme functions(functions.php) file. Make sure your cursor is at the bottom of the file.

Then you copy the code snippet below and paste it into the bottom of your Theme functions(functions.php) file.

add_filter('wp_check_filetype_and_ext', function ($data, $file, $filename, $mimes) {
    $filetype = wp_check_filetype($filename, $mimes);
    return [
	'ext'             => $filetype['ext'],
	'type'            => $filetype['type'],
	'proper_filename' => $data['proper_filename']
    ];
}, 10, 4);

function cc_mime_types($mimes) {
    $mimes['json'] = 'text/plain';
    return $mimes;
}
add_filter('upload_mimes', 'cc_mime_types');

This is the exact same way you do it if you want to enable uploading SVG files. You can read about how to enable SVG upload in this article.

Method 3: WP-Config.php

The third method is by editing your global WP-Config.php. This you achieve by adding the following line in wp-config.php: define(‘ALLOW_UNFILTERED_UPLOADS’, true); – This will allow all file types being uploaded.

define('ALLOW_UNFILTERED_UPLOADS', true);

For this process you will need an FTP client like FileZilla and then sign onto you server through FTP or SFTP.

This is not a way I will recommend you as you disable a lot of security features around uploading files. But if you have a secure sites with no one interfearing, or you just want to upload a lot of different file types, then it can be a solution.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *