Sharing some useful tips, solutions and notes for Geeks.

Thursday, August 27, 2020

No Access-Control-Allow-Origin header is present on the requested resource issue Wordpress

There will come situations like we need to make ajax request to some other domains with wordpress and we face issue related to it.


When you make request, you will get issue like below in the console:


Access to XMLHttpRequest at 'https://example.co.uk/wp-admin/admin-ajax.php' from origin 'https://site1.example.co.uk' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.


This is because of the No Access Control Allow Orgin settings we have in the main site. We can solve this by adding fix in apache, in .htaccess or via filter in functions file. 


add_filter( 'allowed_http_origins', 'add_allowed_origins' );

function add_allowed_origins( $origins ) {

    $origins[] = 'https://site1.example.co.uk';

    $origins[] = 'https://site2.example.co.uk';

    return $origins;

}



Adding above filter will help recognize the requested URL is a valid one and ready to fullfil the ajax request. Hurray !!!!!

No comments: