I have this wordpress site with a plugin called JSON API. This plugin provides a JSON format for the content that is in the wordpress. I was able to enable CORS on the wordpress by adding header("Access-Control-Allow-Origin: *"); on the php header. But when I tried the url that the JSON API plugin provides the CORS does not work anymore.
This is the wordpress site were I'm doing the tests... I used the test cors website to check if it was working and it is... http://kiwa-app.loading.net/
But when I try with the url that the JSON api provides me, is not working anymore. I'm still have the error No 'Access-Control-Allow-Origin' http://kiwa-app.loading.net/?json=info
I will apreciate some help thanks!!!
I've used a few different WordPress API's - but for those of you using the 'official' WP-API, I had much trouble with this CORS --- and what I found was that between the .htaccess approach and a few others I stumbled upon... adding this to your theme functions.php worked best.
Be sure not to use any combinations of these ( .htaccess, header.php, api.php, functions.php ) as it will be angry at you.
In wordpress goto plugins > JSON API > Edit
From the right hand file selection select
json-api/singletons/api.php
You will need to add the following line
header("Access-Control-Allow-Origin: *");
Your code should look similar to this once done. Adding this line anywhere else might not work as expected.
Ok I finally figured out an easy way...
You just have to add:
On the file api.php, this file is located in wp-content/plugins/json-api/singletons/api.php
I hope it helps more people with the same problem!
Before the response is sent to the browser, we can run two action hooks and insert a new
header()
:The first one runs on every method, and the second one is to target specific methods. Here's an implementation of the first one, with a commented way to find the second:
For anyone who is having this issue with multiple origins
In your server hosting your wordpress site, navigate to ../wp-content/plugins/json-rest-api and from here open the plugin.php file.
In this function
Change the header
To
Hope this helps anyone who was incurring the same issues as I.