Advertisement
geminilabs

Fix CORS policy issue with non-www domain

Feb 9th, 2019
351
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.54 KB | None | 0 0
  1. /**
  2.  * Add the website URL without the www subdomain to the allowed HTTP origins array (fixes CORS issue)
  3.  * Paste this in your active theme's functions.php file.
  4.  * @param array $origins
  5.  * @return array
  6.  */
  7. add_filter( 'allowed_http_origins', function( $origins ) {
  8.     $urls = [admin_url(), home_url()];
  9.     foreach( $urls as $url ) {
  10.         $parsedUrl = parse_url( $url );
  11.         $host = str_replace( 'www.', '', $parsedUrl['host'] );
  12.         $origins[] = 'http://'.$host;
  13.         $origins[] = 'https://'.$host;
  14.     }
  15.     return array_unique( $origins );
  16. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement