Form could not be imported. File type must be JSON.


I’ve received this error:
– Form could not be imported. File type must be JSON. –
when I tried to import the .json file exported and running from another website.

This happened to some forms, others were imported without a problem. The were all exported as .json files.
After some research on the internet I came across this great blog post by Torsten Landsiedel (in German, but can be translated):
https://torstenlandsiedel.de/2019/01/28/kaputten-import-bei-caldera-forms-reparieren/.

In the latest WordPress version, some server configurations have a broken import with Caldera Forms. According to Caldera Forms documentation , it is due to the following:

The second error message “Form could not be imported. File type must be JSON. “Which added to Caldera Forms 1.7.5. It indicates that the file you are importing is not considered a JSON file by WordPress. The WordPress checks this changed in WordPress 5.0.1 in a way that was not backwards compatible. If you experience this error, please try exporting again. Most likely this will happen if you created the JSON file manually. This error will not happen if you create the JSON file in Atom or VSCode.

Another reason for you may not be able to write your JSON file. While you can export to a form as PHP, it is not possible to import the export file using the import button.

The error also occurs with a file exported by Caldera Forms. The plugin was in the same version on both websites. The JSON file was neither created manually nor were there other editors in use and of course JSON was chosen for the export and not the PHP version.

/**
 * Support for 'text/html' as the secondary(?) mime type of .json files
 */
add_filter( 'wp_check_filetype_and_ext', 'wpse323750_secondary_mime', 99, 4 );    
function wpse323750_secondary_mime( $check, $file, $filename, $mimes ) {
    if ( empty( $check['ext'] ) && empty( $check['type'] ) ) {
        // Adjust to your needs!
        $secondary_mime = [ 'json' => 'text/html' ];
        // Run another check, but only for our secondary mime and not on core mime types.
        remove_filter( 'wp_check_filetype_and_ext', 'wpse323750_secondary_mime', 99, 4 );
        $check = wp_check_filetype_and_ext( $file, $filename, $secondary_mime );
        add_filter( 'wp_check_filetype_and_ext', 'wpse323750_secondary_mime', 99, 4 );
    }
    return $check;
}

After adding this code to my functions.php it worked and all forms got imported without any issues.

Leave a Reply

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