CORS with php (Wordpress)

CORS with php (Wordpress)

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Fixing CORS Font Loading Issues in WordPress

When fonts fail to render across major browsers like Chrome, Firefox, and IE, the culprit is often a misconfigured Cross-Origin Resource Sharing (CORS) policy. The error log indicates that the browser blocked access because the server responded without the Access-Control-Allow-Origin header. This usually happens when a resource is requested from one domain but served by another, or if a single application attempts to load assets from a different origin than its own root.

In your specific scenario, you moved a WordPress installation from a subdirectory to the root of your domain. While moving files manually—such as copying index.php to the root—can resolve routing issues, it can inadvertently break asset loading if the server's internal rewrite rules or URL structures are no longer aligned with how the browser perceives the request.

Why Simple Header Sets Fail on WordPress

You attempted to fix the issue by adding headers directly to header.php or wp-blog-header.php. While this approach works for standalone PHP applications, it is ineffective in a WordPress environment. The reason lies in how WordPress initializes. When you call header() early in the execution flow (e.g., inside functions.php), the framework may have already processed its own hooks or the specific theme might be outputting content before your header sets are finalized, or they get overridden by subsequent logic.

Furthermore, setting headers via PHP code is static. If you replace the wildcard * with your specific domain URL, you must ensure that the server environment supports sending these headers before any HTML output is sent to the client. In WordPress, relying on manual header injection in theme files is generally discouraged because updates or child theme changes can easily overwrite your custom PHP logic.

The Correct Approach: Using .htaccess

For Apache-based servers, which are common with standard WordPress hosting, the most robust solution is to handle CORS at the server configuration level using an .htaccess file or a virtual host configuration. This ensures that headers are sent regardless of which theme is active or how WordPress initializes its core files.

Place the following directives in your web root's .htaccess file:

<IfModule mod_headers.c>
    Header set Access-Control-Allow-Origin "*"
    Header set Access-Control-Allow-Methods "GET, POST, OPTIONS"
    Header set Access-Control-Allow-Headers "Content-Type, X-Requested-With"
</IfModule>

This configuration allows any origin to access your resources (the * wildcard), which is necessary if you are loading fonts from a CDN or a different subdomain. If you require credentials (cookies), change Access-Control-Allow-Origin to your specific domain and add:

Header set Access-Control-Allow-Credentials true

Note that mod_headers must be enabled for this to work; most modern shared hosting providers have it enabled by default, but older setups might require contacting support.

Addressing the Subdirectory Migration Issue

Your mention of moving WordPress from a subdirectory to the root suggests a deeper structural change. If you copied index.php to the root without updating the rest of the file system or configuring .htaccess rules for rewriting requests like /wp-admin and /wp-login, the application might behave erratically.

However, CORS errors regarding fonts are almost exclusively about the server responding to a preflight request (OPTIONS) or a GET request for an asset (@font-face) without the proper headers. The JavaScript code you posted attempts to fetch a URL using XMLHttpRequest. This is incorrect for loading fonts. Fonts are loaded via CSS rules defined in your stylesheet, not via AJAX calls from JavaScript. Modifying client-side JavaScript to bypass CORS does not fix server-side header requirements; the browser enforces these policies strictly before rendering any resource.

Security Considerations and Limitations

Using Access-Control-Allow-Origin: * is convenient for development or internal tools where security is less critical, but it exposes your assets to any website on the public internet. If you are serving sensitive data or proprietary fonts, restrict this header to your specific domain:

Header set Access-Control-Allow-Origin "http://www.obstgut-auf-der-heide.de"

Be cautious when enabling credentials (Access-Control-Allow-Credentials: true). Browsers do not allow mixing wildcard origins with credentials; you must specify an exact origin in that case. If you need to support both scenarios, consider using a proxy pattern or restricting access strictly to your primary domain.

For developers looking for deeper integration patterns or enterprise-grade solutions for cross-origin requests, LaravelCompany offers insights into how modern frameworks handle these headers securely within their request lifecycle management systems. Understanding these architectural differences helps avoid the pitfalls of manual header manipulation in dynamic environments like WordPress.

Our network