Enable CORS on JSON API Wordpress

前端 未结 10 2019
南笙
南笙 2020-12-03 03:28

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 wordp

相关标签:
10条回答
  • 2020-12-03 03:51

    Ok I finally figured out an easy way...

    You just have to add:

         <? header("Access-Control-Allow-Origin: *"); ?>
    

    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!

    0 讨论(0)
  • 2020-12-03 03:52

    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.

    <?php
    header("Access-Control-Allow-Origin: *"); 
    class JSON_API {
    
      function __construct() {
        $this->query = new JSON_API_Query();
        $this->introspector = new JSON_API_Introspector();
        $this->response = new JSON_API_Response();
        add_action('template_redirect', array(&$this, 'template_redirect'));
        add_action('admin_menu', array(&$this, 'admin_menu'));
        add_action('update_option_json_api_base', array(&$this, 'flush_rewrite_rules'));
        add_action('pre_update_option_json_api_controllers', array(&$this, 'update_controllers'));
      }
    
      function template_redirect() {
    
    0 讨论(0)
  • 2020-12-03 03:52

    In WordPress project go to following file and do like this:

    In wp-includes/rest-api.php change:

    header( 'Access-Control-Allow-Origin: ' . $origin );
    

    to:

    header( 'Access-Control-Allow-Origin: *' );
    

    In wp-includes/http.php change:

    header( 'Access-Control-Allow-Origin: ' . $origin );
    

    to:

    header( 'Access-Control-Allow-Origin: *' );
    
    0 讨论(0)
  • 2020-12-03 03:58

    Now that REST API is merged with core, we can use the rest_api_init action.

    add_action( 'rest_api_init', function()
    {
        header( "Access-Control-Allow-Origin: *" );
    } );
    
    0 讨论(0)
提交回复
热议问题