JSON is a JavaScript Object Notation file used for data transmission. Generally, theme/plugin developers use JSON configuration file for importing/exporting customizer settings. However, importing or uploading JSON file works only when the developer offers a custom settings page. By default, WordPress will prevent you from uploading JSON files though Media Library or post editor. If you are in a need of uploading JSON files in WordPress, here are the options for you.
Default JSON File Upload Behavior in WordPress
Go to “Media > Library” section in your WordPress admin panel and try to upload a .json file. You will see an error mentioning, “Sorry, you are not allowed to upload this file type”. WordPress restricts uploading certain file types like SVG and JSON due to security reasons. As these are text files, the uploader can easily add malicious code in the file to harm your site. However, sometimes you may create the JSON file yourself or know the file is from clean source. In these cases, you can manually enable JSON upload either for processing the file or allow users to download from the frontend.
Theme of Plugin JSON Settings Import
As mentioned, almost all commercial themes and plugins offer an option to export the settings and import them in another site. This setting file will be in .json format and you can simply upload the file through theme or plugin’s setting page. For this, you do not need any additional plugin or code snippet. Below is an example screenshot from the popular GeneratePress premium theme for importing/exporting customizer settings as a JSON file.
Some themes like Astra offers a custom plugin for importing/exporting customizer settings. In this article, we will discuss uploading JSON file through Media Library or post editor and NOT the theme/plugin settings import.
Upload JSON Files in WordPress Admin Panel
When uploading a file, WordPress checks its MIME type to allow or restrict the upload. WordPress has a list of allowed file types as listed in this official codex documentation and .json is not the allowed format. So, you have to manually add the MIME type for JSON for allowing the file uploads through admin panel. Otherwise, you can directly upload the file on your server using FTP.
1. Allow JSON Upload Using a Plugin
This is the best option to upload JSON files in your site.
- Install and activate “WP Extra File Types” plugin on your site. Check out our article to learn more on how to install a plugin in WordPress site.
- Navigate to “Settings > Extra File Types” menu to see a huge list of file types.
- Scroll down to find .json file type and enable the check mark. Since the list is huge, you can use “Control + F” or “Command + F” to search for JSON and find the line quickly.
- Scroll down to the bottom and click “Save Changes” button.
- Now, go to “Media > Add New” menu and upload your JSON files.
- You will see the file is uploaded successfully without any error.
2. Allow JSON Upload Using Custom Function
You can use a custom function to allow MIME type for uploading JSON file format.
- Go to “Appearance > Theme File Editor” section (or “Tools > Theme File Editor” in block themes).
- Make sure to select your active theme from the list and click on “Theme Functions” (functions.php) file from the list.
- Copy the following code and paste at the end of the file.
// Allow JSON Upload
function allow_json_mime($mimes) {
$mimes['json'] = 'application/json';
return $mimes;
}
add_filter('upload_mimes', 'allow_json_mime');
- Click “Upload File” button to save your changes.
Now, upload a JSON file and you will see the file can be uploaded successfully.
Remember to use child theme for adding new functions or use plugins like Code Snippets for this purpose. You can learn more on how to edit theme functions and add custom functions in WordPress.
3. Allow All MIME Types Using wp-config.php File
As mentioned, WordPress filters or restricts certain file types from uploading. However, you can disable this feature and allow uploading of all file types.
- Remotely login to your hosting server using FTP account or open to File Manager app in your hosting panel.
- Find wp-config.php file in the root installation directory and edit it using a text editor. Learn more on how to edit wp-config.php file in WordPress.
- Copy the following code and paste at the end of your file.
- Save the changes.
/** Allow All MIME File Types */
define( 'ALLOW_UNFILTERED_UPLOADS', true );
By default, this feature works only for administrator role in single site installation and super admin role in multi-site installation. So, it is a good idea to use this option when you are the only administrator of your site and restrict others from uploading JSON files.
Final Words
Using Extra File Types is an easy option to upload JSON and any other file type. The plugin also offers an option to use custom MIME type for a file extension so that you can add a new item if it is not appearing the list. However, the plugin does not provide the control over the user roles and allow everyone to upload JOSN files. This is the same issue in using custom theme function. If you only want the administrators to have this feature, then using wp-config.php is the suitable option.
1 Comment
Leave your reply.