plugin cors whitelist domain name

create new plugin wordpress cors whitelist domain name public function restrict_access_by_domain() { $allowed_domains = $this->get_whitelisted_domains(); $current_domain = $_SERVER['HTTP_HOST']; // Get the current domain from the server variable // Check if the current domain or its subdomain is in the allowed domains $is_allowed = false; foreach ($allowed_domains as $allowed_domain) { $parsed_url = parse_url($allowed_domain); $domain_name = $parsed_url['host']; if ($current_domain === $domain_name || preg_match("/\b$domain_name\b$/i", $current_domain)) { $is_allowed = true; // Mark the domain as allowed header("Access-Control-Allow-Origin: $allowed_domain"); header("Access-Control-Allow-Methods: GET, POST, OPTIONS"); header("Access-Control-Allow-Credentials: true"); header("Access-Control-Allow-Headers: Content-Type, Accept, Authorization"); break; } } if (!$is_allowed) { header("HTTP/1.1 403 Forbidden"); header("Content-Type: text/plain"); echo 'Access denied. Your domain is not authorized to view this site.'; exit(0); } } private function get_whitelisted_domains() { $args = array( 'post_type' => 'whitelist-domain', 'post_status' => 'publish', 'posts_per_page' => -1, ); $whitelist_domains = get_posts($args); $home_url = home_url(); $domains = [$home_url]; foreach ($whitelist_domains as $post) { $domain = get_post_meta($post->ID, '_whitelist_domain', true); if ($domain) { $domains[] = $domain; } } return $domains; } please provide solution for fixed, why it not work?

Comment (0)

You’ll be in good company