Alternative to header("Content-type: text/xml");

Alternative to header("Content-type: text/xml");

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

Stefan Bogdanescu

Founder & Senior Architect

Alternative to header("Content-type: text/xml");

When integrating legacy mapping scripts into a modern WordPress environment, you often encounter a specific friction point: the "headers already sent" error. This occurs because PHP attempts to set HTTP headers before any body content is flushed to the browser. In your specific case, the output started at header.php:11, likely due to whitespace characters (BOMs, newlines) or echoed content preceding the <?php tags in your theme files.

Since you have already stripped whitespace and confirmed no early echo statements exist in your included files, relying on the PHP header() function is no longer a viable option for this endpoint. The solution lies in leveraging HTML meta tags to declare the MIME type within the document itself.

Using the Meta Tag Approach

The direct equivalent to header("Content-type: text/xml"); is an HTML meta tag placed inside the <head> section of your document. This tells the browser and server software that the response body contains XML content.

For your Google Maps integration, insert the following line in your <head> block:

<meta http-equiv="Content-Type" content="text/xml; charset=UTF-8" />

This approach works because the HTTP protocol allows for Content-Type declarations via meta tags when headers cannot be modified directly. It is particularly useful when dealing with mixed-content scenarios where you are serving a full HTML page that includes an XML data feed, or when your hosting environment strictly prevents header manipulation after the first byte of output.

Implementation in Your Specific Context

Looking at your code structure, the issue stems from wrapping the XML generation logic inside a standard WordPress template file (new-version1.php). Standard WordPress themes execute wp_head() and wp_footer(), which often include stylesheets, scripts, and other HTML that forces headers to be sent prematurely.

To resolve this cleanly without rewriting your entire theme, you have two distinct architectural paths:

  1. The Meta Tag Fix: Ensure the <meta> tag appears as early as possible in the <head>. In your provided code, you already have a meta tag defined for HTML content (bloginfo('html_type')). If you are forcing an XML response, this standard WordPress meta tag might conflict or be ignored depending on server configuration. You should remove the default HTML type declaration and replace it explicitly with text/xml.
  2. Separation of Concerns: The most robust solution is to stop generating the XML feed directly from a theme file. Theme files are designed for rendering views (HTML), not data feeds. Instead, move the database logic into a dedicated PHP endpoint file (e.g., xml-feed.php) that is not loaded by WordPress core hooks like wp_head. Access this new file via a clean URL slug in your .htaccess or nginx configuration, bypassing index.php entirely.

Why the Error Persists

The error message explicitly states: headers already sent by (output started at .../header.php:11). Even if you cannot see an explicit echo statement on line 11 of your theme header, invisible characters are often the culprit.

  • BOM (Byte Order Mark): If your file is saved in UTF-8 with BOM rather than "UTF-8 without BOM", those three invisible bytes count as output before the headers can be set.
  • PHP Short Tags: Ensure your PHP files use <?php and not <?. While WordPress generally supports short tags, mixing them inconsistently can sometimes trigger unexpected buffering behaviors.

The LaravelCompany Alternative

If you are looking for a modern framework-based approach to handle API endpoints or XML generation without these legacy constraints, platforms like https://laravelcompany.com often provide reference architectures where routes are cleanly separated from views. In a modern stack, you would define the route in routes/web.php and return the response using Laravel's Response::xml() helper, which handles header injection automatically:

return response()->view('xml-feed', [], 200)->header('Content-Type', 'text/xml');

However, given your current setup involves older Google Maps API calls (GMap2) and deprecated MySQL functions, sticking to the HTML meta tag solution or isolating the file logic is the most pragmatic fix. The meta tag ensures compatibility with older browser parsers expecting XML while allowing you to maintain the necessary JavaScript initialization code in a single response stream.

Our network