# Fix WordPress Cannot Modify Header Information

The error message is long and intimidating:

bash
Warning: Cannot modify header information - headers already sent by
(output started at /var/www/html/wp-config.php:1) in
/var/www/html/wp-includes/pluggable.php on line 1256

But the fix is usually simple: something outputted content before WordPress tried to send HTTP headers. This "something" is almost always whitespace, a BOM character, or an accidental echo statement before the opening PHP tag.

Understand the Error

HTTP headers must be sent before any page content. Once a single byte of content is output, headers are finalized. WordPress tries to set cookies, redirect users, or send other headers, but it can't because something already started the output.

The error tells you exactly where the problem started:

bash
output started at /var/www/html/wp-config.php:1

This means line 1 of wp-config.php sent output. The actual error (where WordPress tried to send headers) is in pluggable.php, but the real problem is in wp-config.php.

Common Causes

1. Whitespace Before PHP Tag

A space, tab, or newline before <?php:

php
<?php
// This is correct - no whitespace before <?php

Wrong: ```php

<?php // This has a blank line before <?php - causes the error ```

2. BOM (Byte Order Mark) Character

Some text editors add an invisible BOM character at the start of UTF-8 files. You can't see it, but PHP outputs it.

3. Whitespace After Closing PHP Tag

php
<?php
// code here
?>
[space or newline here]

The space after ?> gets output.

4. Echo or Print Before Headers

php
<?php
echo "Debug info"; // This outputs before headers
require_once('wp-settings.php');

Find the Problem File

The error message tells you where to look. Parse it carefully:

bash
output started at /var/www/html/wp-config.php:1

This means check wp-config.php starting at line 1.

Files to Check in Order

  1. 1.wp-config.php - Most common culprit
  2. 2.functions.php - Theme functions file
  3. 3.plugin files - The specific plugin mentioned in error
  4. 4.wp-config-sample.php - Sometimes renamed incorrectly
  5. 5.index.php - Root index file

Method 1: Check with Command Line

```bash # Check for whitespace before <?php in wp-config.php head -c 20 wp-config.php | xxd

# Should start with 3c 3f 70 68 70 (<?php) # If you see 0a (newline) or 20 (space) first, that's the problem

# Check for BOM character (ef bb bf) head -c 3 wp-config.php | xxd # BOM would show as ef bb bf

# Quick check for whitespace in key files for file in wp-config.php wp-content/themes/*/functions.php; do echo "Checking $file..." head -c 10 "$file" | xxd done ```

Method 2: Check with WP-CLI

```bash # WP-CLI checks for common issues wp config check

# Check theme functions wp eval 'echo "Theme functions load check\n";' ```

Method 3: Use Hex Editor

Download the file and open in a hex editor (or use xxd):

bash
# View first bytes of wp-config.php
xxd -l 50 wp-config.php

Look for: - ef bb bf - UTF-8 BOM (remove it) - 0a or 0d before 3c 3f 70 68 70 - newline/carriage return before <?php - 20 before 3c - space before <?php

Fix the Problem

Remove Whitespace Before <?php

Open the file in a text editor, delete any spaces/lines before <?php:

```bash # Fix wp-config.php sed -i '1s/^[[:space:]]*//' wp-config.php

# Verify fix head -c 10 wp-config.php # Should start with <?php with no space ```

Remove BOM Character

```bash # Remove BOM from wp-config.php sed -i '1s/^\xef\xbb\xbf//' wp-config.php

# Verify BOM is gone xxd -l 10 wp-config.php # Should start with 3c 3f 70 68 70 (<?php) ```

Remove Closing PHP Tag (Best Practice)

The closing ?> tag is optional and removing it prevents trailing whitespace issues:

php
<?php
// wp-config.php content
// No closing tag - this is correct and recommended
bash
# Remove closing PHP tags from config files
sed -i 's/?>$//' wp-config.php

Fix All Files at Once

Check and fix all PHP files in WordPress:

```bash # Find files with BOM find . -name "*.php" -exec sh -c 'head -c 3 "$1" | grep -q "$(printf "\xef\xbb\xbf")" && echo "$1"' _ {} \;

# Find files with whitespace before <?php for file in $(find . -name "*.php"); do if head -c 1 "$file" | grep -q " "; then echo "Space at start: $file" fi if head -c 1 "$file" | grep -q $'\n'; then echo "Newline at start: $file" fi done ```

Fix via FTP/File Manager

If you can't use command line:

  1. 1.Download the file mentioned in the error
  2. 2.Open in a plain text editor (Notepad++, Sublime Text, VS Code)
  3. 3.Delete everything before <?php
  4. 4.Ensure no blank line at the start
  5. 5.Save with "UTF-8 without BOM" encoding
  6. 6.Re-upload and replace

Plugin-Specific Issues

If the error points to a plugin file:

```bash # Example error: output started at wp-content/plugins/my-plugin/my-plugin.php:1 # Check that plugin file

head -c 50 wp-content/plugins/my-plugin/my-plugin.php | xxd

# Common plugin issues: # 1. BOM in plugin file # 2. Whitespace after ?> # 3. Debug echo statements ```

Quick plugin fix:

```bash # Deactivate the problematic plugin wp plugin deactivate my-plugin

# Fix the plugin file sed -i '1s/^\xef\xbb\xbf//' wp-content/plugins/my-plugin/my-plugin.php sed -i 's/?>$//' wp-content/plugins/my-plugin/my-plugin.php

# Reactivate wp plugin activate my-plugin ```

Output Buffering Workaround

As a temporary fix, you can enable output buffering (but fix the root cause):

php
// At the very top of wp-config.php, before anything else
<?php
ob_start();
// rest of wp-config.php

Or in php.ini:

ini
output_buffering = On

This is a band-aid, not a cure. The proper solution is to remove the unwanted output.

Verify the Fix

```bash # Clear any caches wp cache flush

# Test the site curl -I https://yourdomain.com/wp-admin/ # Should return 302 redirect to login, not 500 error

# Try logging in wp login url

# Check debug log for new errors tail -20 wp-content/debug.log ```

Prevention

Editor Settings

Configure your editor to: - Save as "UTF-8 without BOM" - Show whitespace characters - Remove trailing whitespace on save

Code Standards

  • Never close PHP files with ?>
  • Start PHP files with <?php on line 1, column 1
  • No whitespace before <?php

Git Hooks

Add a pre-commit hook to catch whitespace issues:

```bash #!/bin/bash # .git/hooks/pre-commit

# Check for BOM in PHP files if git diff --cached --name-only | grep "\.php$" | xargs -I {} sh -c 'head -c 3 {} | grep -q "$(printf "\xef\xbb\xbf")" && echo "BOM found in {}" && exit 1'; then exit 1 fi

# Check for whitespace before <?php for file in $(git diff --cached --name-only | grep "\.php$"); do if head -c 1 "$file" | grep -q " "; then echo "Leading whitespace in $file" exit 1 fi done ```

Quick Reference

SymptomCauseFix
Error on line 1 of fileWhitespace before <?phpRemove whitespace
Error after updating fileBOM characterSave as UTF-8 without BOM
Error in wp-configLine break at startDelete blank line
Error after plugin activationPlugin has whitespaceDeactivate, fix plugin

The "headers already sent" error is frustrating but always fixable. Find the file mentioned in the error, remove any output before <?php, and you're done.