How to Force an HTML Header Response in WordPress using PHP

Sometimes you need to ensure that your WordPress server sends a specific header response – such as the Content-Type header.

This is crucial when you need to ensure that the content you’re serving is interpreted correctly by user browsers.

How? Follow the steps below:

  1. Access Your Theme’s Functions File
    • Navigate to your WordPress Dashboard.
    • Go to Appearance > Theme Editor.
    • On the right-hand side, click on the functions.php file.
  2. Add the Custom Header Code
    • At the top of the functions.php file, before any other code, paste the following:
      header("Content-Type: text/html; charset=UTF-8");
  3. Save Changes
    • Click the Update File button to save your changes.

Note: the header() function must be called before any output is sent, whether it’s from PHP, HTML, or whitespace. Ensure there’s no content sent before the header() function call, or you may encounter “headers already sent” errors.