Skip to content

Instantly share code, notes, and snippets.

@modemlooper
Created June 14, 2022 00:17
Show Gist options
  • Save modemlooper/71deead13a8bb1b60c8cc6eb820432e5 to your computer and use it in GitHub Desktop.
Save modemlooper/71deead13a8bb1b60c8cc6eb820432e5 to your computer and use it in GitHub Desktop.
allow cors wp-api
/**
* 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