Blog / WordPress/ How to Fix "Warning: Cannot Modify Header Information - Headers Already Sent" in WordPress (2026 Guide)

How to Fix "Warning: Cannot Modify Header Information - Headers Already Sent" in WordPress (2026 Guide)

This is one of the most frustrating errors in WordPress development. You're happily working on your site, and then suddenly you see:

Warning: Cannot modify header information - headers already sent by (output started at /path/to/file.php:123)

What Causes This Error?

HTTP headers must be sent before any HTML output. In PHP, functions like header(), setcookie(), and wp_redirect() need to send headers first. If anything (even a space or newline) is output before these functions run, PHP throws this error.

Common Causes:

  • UTF-8 BOM - Your PHP file has a Byte Order Mark at the beginning
  • Whitespace - Spaces or newlines before <?php or after ?>
  • Plugin/Theme errors - Plugins outputting content prematurely
  • Debug mode notices - PHP warnings appearing before headers

5 Solutions to Fix This Error

1. Remove UTF-8 BOM (Most Common Fix)

Many editors add a BOM to UTF-8 files, which creates invisible characters. Open your file in a code editor like VS Code or Notepad++:

// In VS Code:
// Click "Encoding" at bottom right -> "Save with Encoding" -> "UTF-8 without BOM"

// In Notepad++:
// Encoding -> Convert to UTF-8 without BOM

2. Check for Whitespace

Make sure your PHP files look like this:

<?php
// Your code here
// No whitespace before or after

Remove the closing ?> tag at the end of files (this is WordPress best practice).

3. Enable Output Buffering

Add this to your theme's functions.php:

function cmp_do_output_buffer() {
    ob_start();
}
add_action('init', 'cmp_do_output_buffer');

4. Fix wp_redirect() Issues

If you're using wp_redirect(), ensure it's called before any output:

// WRONG:
echo "Loading...";
wp_redirect(home_url());

// CORRECT:
wp_redirect(home_url());
exit;

5. Check wp-config.php

Make sure there's no whitespace before <?php:

<?php
// Database settings
define('DB_NAME', 'database_name');
// ... rest of config

Prevention Tips

  • Always use UTF-8 without BOM encoding
  • Never close PHP tags at end of files
  • Use quality code editors (VS Code, PhpStorm)
  • Enable debug mode on staging sites only
  • Keep plugins and themes updated

Diagnostic Checklist

Check Action
File encoding Save as UTF-8 without BOM
Whitespace Remove spaces before/after PHP tags
Plugin conflict Disable all plugins temporarily
Theme issue Switch to default theme
wp-config.php Verify no whitespace in file

This error is annoying but solvable. The key is identifying which file is causing the output - the error message will tell you exactly which file and line number to check.

Post a Comment

Your email will not be published. Required fields are marked with *.