Enable CORS on JSON API Wordpress

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

    Before the response is sent to the browser, we can run two action hooks and insert a new header():

    do_action("json_api", $controller, $method);
    do_action("json_api-{$controller}-$method");
    

    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:

    add_action( 'json_api', function( $controller, $method )
    {
        # DEBUG
        // wp_die( "To target only this method use 
    add_action('$controller-$method', function(){ /*YOUR-STUFF*/ });
    " ); header( "Access-Control-Allow-Origin: *" ); }, 10, 2 );

提交回复
热议问题