Skip to content

Instantly share code, notes, and snippets.

@lenilsonjr
Created July 26, 2017 19:06
Show Gist options
  • Save lenilsonjr/b3dbf67c5370cf3ac46f7d9960114d92 to your computer and use it in GitHub Desktop.
Save lenilsonjr/b3dbf67c5370cf3ac46f7d9960114d92 to your computer and use it in GitHub Desktop.
Allow CORS on WP-API
add_action( 'rest_api_init', 'handle_preflight' );
function handle_preflight() {
header("Access-Control-Allow-Origin: *"); //Pay attention
header("Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE");
header("Access-Control-Allow-Credentials: true");
header("Access-Control-Allow-Headers: x-requested-with,x-wp-nonce");
if ( 'OPTIONS' == $_SERVER['REQUEST_METHOD'] ) {
status_header(200);
exit();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment