Loading Google font in HTTPS, content being blocked

Loading Google font in HTTPS, content being blocked

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

Stefan Bogdanescu

Founder & Senior Architect

The HTTPS Mismatch in Google Fonts Integration

When a WordPress theme automatically injects Google Fonts based on user selection, it often encounters a specific security hurdle: mixed content warnings. If your site is served over HTTPS but the font stylesheet loads via HTTP, browsers will block the request entirely to protect user data from man-in-the-middle attacks. The console log explicitly states this: "The page at 'https://www.example.com/' was loaded over HTTPS, but ran insecure content from 'http://fonts.googleapis.com/...'".

The core issue usually lies not in how Google Fonts is called, but in how the server determines the protocol prefix. In the provided code snippet, the logic attempts to set $prefix based on $_SERVER['HTTPS']. While this check often works for sites hosted behind a standard load balancer or reverse proxy where PHP runs directly on the web server, it frequently fails when the application sits behind a modern reverse proxy like Nginx or Cloudflare.

In these environments, the reverse proxy terminates SSL before passing the request to your PHP application. Consequently, $_SERVER['HTTPS'] defaults to an empty string or "off" because the connection reaching the PHP process is already unencrypted. The variable $prefix remains hardcoded to "http", resulting in the insecure mix of content that triggers the block.

To resolve this without modifying the underlying theme logic (which might be third-party or difficult to edit), you should leverage headers passed by your reverse proxy. Most modern proxies set $_SERVER['HTTP_X_FORWARDED_PROTO'] to "https" when the original request was secure. You can update the font injection logic to prioritize this header:

$prefix = "http";

// Check if the connection is already encrypted directly (legacy setups)
if(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') {
    $prefix = "https";
} 
// Check for reverse proxy headers (modern setups)
elseif(isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https') {
    $prefix = "https";
}

if($get_google_font){
    if(!in_array($rule_split[0], $this->used_fonts)){
        // Construct the secure URL based on the determined prefix
        $fullUrl = $prefix . "://fonts.googleapis.com/css?family=".str_replace(' ','+',$rule_split[0]).$font_weight;
        
        $this->extra_output .= "\n<!-- google webfont font replacement -->\n";
        $this->extra_output .= '<link id="google_webfont_'.$this->webfont_count.'" rel="stylesheet" type="text/css" href="'.$fullUrl.'" />'."\n";
    }
}

This approach ensures that regardless of whether PHP sees a direct encrypted connection or a proxied one, the generated URL reflects the actual security context of the user's browser.

However, if you have full control over the theme and can modify the functions.php or the font loader file directly, a more robust pattern exists. Instead of relying solely on server-side variables, you can force the protocol by checking the global configuration or using a helper function that inspects both $_SERVER['HTTPS'] and $_SERVER['HTTP_X_FORWARDED_PROTO'].

For teams managing large WordPress ecosystems, standardizing this behavior is critical. A common pitfall is assuming that because the site URL looks secure in the admin bar, all output will be secure. The distinction between the application's view of the connection and the client's view is where mixed content errors originate. Ensuring the href attribute in the generated <link> tag always matches the scheme of the page it resides on eliminates these warnings entirely.

If you are building a custom theme or plugin, consider encapsulating this logic in a utility class. This prevents accidental regression when switching hosting environments from shared hosting to managed cloud infrastructure. The principle remains consistent: never serve HTTP resources on an HTTPS page.

Our network