-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathplugin.php
39 lines (35 loc) · 1012 Bytes
/
plugin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?php
/**
* Plugin Name: WP-REST-Allow-All-CORS
* Plugin URI: http://AhmadAwais.com/
* Description: Allow all cross origin requests to your WordPress site's REST API.
* Author: mrahmadawais, WPTie
* Author URI: http://AhmadAwais.com/
* Version: 1.0.0
* License: GPL2+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
*
* @package WPRAC
*/
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
// Hook.
add_action( 'rest_api_init', 'wp_rest_allow_all_cors', 15 );
/**
* Allow all CORS.
*
* @since 1.0.0
*/
function wp_rest_allow_all_cors() {
// Remove the default filter.
remove_filter( 'rest_pre_serve_request', 'rest_send_cors_headers' );
// Add a Custom filter.
add_filter( 'rest_pre_serve_request', function( $value ) {
header( 'Access-Control-Allow-Origin: *' );
header( 'Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE' );
header( 'Access-Control-Allow-Credentials: true' );
return $value;
});
} // End fucntion wp_rest_allow_all_cors().