Enable CORS on JSON API Wordpress

前端 未结 10 2106
南笙
南笙 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:49

    Using Wordpress 5.2.3 - whilst using GET and POST externally, the following finally opened sesame for me. I tried all of the answers above (to no avail) before finding this solution that worked for my case.

    add_action( 'rest_api_init', function () {
        add_action( 'rest_pre_serve_request', function () {
            header( 'Access-Control-Allow-Headers: Authorization, Content-Type, X-WP-Wpml-Language', true );
            header("Access-Control-Allow-Origin: *");
        } );
    }, 15 );
    

    Hopefully WordPress will have an official doggy door-flap for CORS control in the future.

提交回复
热议问题