Enable CORS for getting an inline SVG by URL
TLDR; Is there a way I can enable CORS for SVGs in my WordPress back-end, so I can get them through a http request to a different domain? Ideally without having to touch .htaccess
?
What I'm aiming for
I am building a headless WordPress/React site. I would like to have my SVGs, uploaded to WP back-end, inlined into my front-end code (so I can access their DOM).
I have URL to the SVG and react-inlinesvg
library allows me to inline it. I am getting No 'Access-Control-Allow-Origin' header is present on the requested resource.
error however. react-inlinesvg
makes the actual http request through httpplease
library.
What I've tried
At first, I had to enable upload of SVGs to the admin.
add_filter('upload_mimes', function($mimes) {
return array_replace($mimes, [
'svg' = 'image/svg+xml',
'svgz' = 'image/svg+xml'
]);
});
I've found those filters as well, but I don't think they are being fired if the file is requested via http:
add_action('rest_api_init', function($wp) {
remove_filter( 'rest_pre_serve_request', 'rest_send_cors_headers' );
add_filter( 'rest_pre_serve_request', function( $value ) {
header( 'Access-Control-Allow-Origin: *' );
header( 'Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE' );
header( 'Access-Control-Allow-Credentials: true' );
return $value;
});
});
add_filter('wp_headers', function($headers) {
$headers['Access-Control-Allow-Origin'] = '*';
return $headers;
});
Thanks in advance!