Want to cache json with wp-super cache in WordPress? Use following code in your theme’s functions.php file OR in site specific plugin.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | /** * Tell WP Super Cache to cache API endpoints * * @param string $eof_pattern * * @return string */ function wcorg_json_cache_requests( $eof_pattern ) { global $wp_super_cache_comments; if ( defined( 'JSON_REQUEST' ) && JSON_REQUEST ) { // Accept a JSON-formatted string as an end-of-file marker, so that the page will be cached $json_object_pattern = '^[{].*[}]$'; $json_collection_pattern = '^[\[].*[\]]$'; $eof_pattern = str_replace( '<\?xml', sprintf( '<\?xml|%s|%s', $json_object_pattern, $json_collection_pattern ), $eof_pattern ); // Don't append HTML comments to the JSON output, because that would invalidate it $wp_super_cache_comments = false; } return $eof_pattern; } add_filter( 'wp_cache_eof_tags', 'wcorg_json_cache_requests' ); |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.