headers - allowed_http_origins() only works in theme functions.php

The following code works in my theme's functions.php file but not in my plugin's mainentry file:** Allow cro

The following code works in my theme's functions.php file but not in my plugin's main/entry file:

/** Allow cross origin resource access
 */
function add_cors_http_header(){
  header("Access-Control-Allow-Origin: *");
}
add_action('init','add_cors_http_header');

If put into the main plugin file I see this:

Access to XMLHttpRequest at 'http://localhost/
wptest2/wp-admin/admin-ajax.php' 
from origin 'http://localhost:8080' 
has been blocked by CORS policy: 
No 'Access-Control-Allow-Origin' header 
is present on the requested resource.

I need to setup CORS using my plugin files only so users won't have to make custom mods, is there any way to accomplish this?

Also tried in the plugin entry file (and failed):

function add_allowed_origins(array $origins = []) {
  $origins[] = 'http://localhost:8080';
  return $origins;
}
add_filter('allowed_http_origins', 'add_allowed_origins');

.

function add_cors_http_header() {
  header("Access-Control-Allow-Origin: *");
}
add_action('send_headers', 'add_cors_http_header');

The following code works in my theme's functions.php file but not in my plugin's main/entry file:

/** Allow cross origin resource access
 */
function add_cors_http_header(){
  header("Access-Control-Allow-Origin: *");
}
add_action('init','add_cors_http_header');

If put into the main plugin file I see this:

Access to XMLHttpRequest at 'http://localhost/
wptest2/wp-admin/admin-ajax.php' 
from origin 'http://localhost:8080' 
has been blocked by CORS policy: 
No 'Access-Control-Allow-Origin' header 
is present on the requested resource.

I need to setup CORS using my plugin files only so users won't have to make custom mods, is there any way to accomplish this?

Also tried in the plugin entry file (and failed):

function add_allowed_origins(array $origins = []) {
  $origins[] = 'http://localhost:8080';
  return $origins;
}
add_filter('allowed_http_origins', 'add_allowed_origins');

.

function add_cors_http_header() {
  header("Access-Control-Allow-Origin: *");
}
add_action('send_headers', 'add_cors_http_header');
Share Improve this question edited Jul 12, 2019 at 21:22 Sean D asked Jul 12, 2019 at 20:29 Sean DSean D 3878 silver badges21 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

Try using send_headers action hook instead of init. It should work.

add_action( 'send_headers', 'add_cors_http_header' );

function add_cors_http_header()
{
    header("Access-Control-Allow-Origin: *");
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745325761a4622648.html

相关推荐

  • headers - allowed_http_origins() only works in theme functions.php

    The following code works in my theme's functions.php file but not in my plugin's mainentry file:** Allow cro

    7小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信