By default, WordPress blocks JSON file uploads in the media library. While some themes and plugins let you import settings through their options page, this doesn’t mean you can upload JSON files via the media library.
In this article, we will show how to allow and upload JSON files in WordPress directly through the media library using a dedicated plugin or custom code, without relying on theme settings.
Let’s begin.
Method 1: Allow JSON Upload in WordPress Using a Plugin
As we have said before WordPress doesn’t natively allow uploading JSON files. You have to use a plugin with a custom setting for JSON file upload. In this case, we are using the DiviFlash Divi plugin, which is one of the best options for uploading JSON files to your site. Here’s how to do it:
First, purchase the DiviFlash plugin from their websites. Then navigate to your WordPress Dashboard and follow the instructions below,
- Go to Plugins > Add New Plugin.
- Click Upload Plugin and choose Choose File.
- Select the DiviFlash Plugin ZIP file and click Install Now.
- After successful installation, click “Activate” to activate the DiviFlash plugins.
- From your WordPress dashboard, navigate to DiviFlash > Settings > General Settings.
- Now, enable the JSON file upload option.
- Now, navigate to Media > Add New Media and upload a JSON file. It will now upload without any error.
Method 2: Allow JSON Upload in WordPress Using Custom Code
If you prefer a more hands-on approach and are comfortable with the code you can implement JSON upload functionality using custom code. Here’s how to do it:
- In your WordPress dashboard, go to Appearance > Theme File Editor.
- Select functions.php from the list of Theme Files.
- Scroll to the bottom of the code editor and add the code below.
- When finished, click Update File.
Copy the following code and paste it at the end of the file.
function allow_json_mime($mimes) {
$mimes['json'] = 'application/json';
return $mimes;
}
add_filter('upload_mimes', 'allow_json_mime');
Now, navigate to Media > Add New Media and upload a JSON file. It will now upload without any error.
Method 3: Allow All MIME Types Using wp-config.php File
Another option is to enable uploads for all file types, but this poses a serious security risk, allowing malicious files that could harm your website. We strongly recommend against allowing all MIME types in your wp-config.php file. Here’s how to do it,
- Log in to your hosting server via an FTP account or open the File Manager in your hosting control panel.
- Locate the wp-config.php file in the root directory of your WordPress installation.
- Open the wp-config.php file in a text editor, then copy and paste the following code at the end of the file.
- Save the changes.
Copy the following code and paste it at the end of the file.
define('ALLOW_UNFILTERED_UPLOADS', true);
Why Doesn’t WordPress Support JSON File Upload?
JSON files are a flexible data format that can be easily manipulated. If WordPress were to allow unrestricted JSON uploads, malicious users could potentially upload files with malicious code or data that could compromise the website’s security.
To mitigate these risks, WordPress has a built-in mechanism to restrict file uploads to specific MIME types like SVG and JSON, which are essentially file formats. By default, WordPress only allows a limited set of common file types, such as images, documents, and audio/video files.
This is why when you try to upload a .json file through Media you will see this error “Sorry, you are not allowed to upload this file type”.
Conclusion
In conclusion, while there are a few methods to upload JSON files in WordPress, the easiest and most user-friendly option is using a plugin like DiviFlash. This method allows you to safely enable JSON file uploads without touching any code, making it ideal for beginners or those who prefer a hassle-free solution.
Custom code provides more flexibility but requires a deeper understanding of WordPress development. While Modifying wp-config.php allows all MIME types, it’s generally safer to use the previous methods.
Your choice depends on your technical skills and project needs, but for a hassle-free option, a plugin is generally the best choice. If you still face any issues simply comment below or give us a knock. Our dedicated team is always here to help you.
0 Comments