Created
June 14, 2022 00:17
-
-
Save modemlooper/71deead13a8bb1b60c8cc6eb820432e5 to your computer and use it in GitHub Desktop.
allow cors wp-api
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Allow cross domain api access from iOS and Android | |
* | |
* @param WP_Rest_Request $request | |
* @return void | |
*/ | |
function appp_init_cors( $request ) { | |
$origin_url = '*'; | |
header( 'Access-Control-Allow-Origin: ' . $origin_url ); | |
header( 'Access-Control-Allow-Methods: GET,DELETE,POST,PUT,PATCH,OPTIONS' ); | |
header( 'Access-Control-Allow-Credentials: true' ); | |
return $request; | |
} | |
add_action( | |
'rest_api_init', | |
function() { | |
remove_filter( 'rest_pre_serve_request', 'rest_send_cors_headers' ); | |
add_filter( 'rest_pre_serve_request', 'appp_init_cors' ); | |
}, | |
15 | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment