DEV Community

One PHP Warning Emptied the H1 on Every WordPress Blog Post

A client's WordPress site looked fine on the home page and on its product pages. Every blog post, though, had two problems: a PHP warning printed at the top, and an empty <h1>. The cause was one line in the theme's single.php.

The symptom

Open any post and you saw:

  • a PHP warning above the content about accessing an array offset on a value that wasn't an array;
  • an <h1></h1> with nothing inside it.

The empty H1 is the expensive part. Search engines and screen readers use the H1 as the page's main heading. Every article on the blog was effectively untitled.

The cause

The theme was built for its own custom post type. In single.php the title came from the theme's own data array (simplified):

<h1><?php echo $content['title']; ?></h1>
Enter fullscreen mode Exit fullscreen mode

That array is filled for the theme's own content type. On an ordinary blog post it isn't, so $content wasn't an array. PHP raised a warning for $content['title'], returned null, and the heading came out empty.

The fix

Fall back to the standard WordPress title when the theme's field isn't there:

<?php
$mc_title = (is_array($content) && !empty($content['title']))
    ? $content['title']
    : get_the_title();
?>
<h1><?php echo esc_html($mc_title); ?></h1>
Enter fullscreen mode Exit fullscreen mode

Two checks matter. is_array() stops the warning. !empty() catches the case where the field exists but is blank, so you never print an empty heading again. esc_html() is worth adding while you are there.

Do it in a child theme

Editing the parent theme works until the next theme update overwrites it. Copy single.php into a child theme and change it there. On this site we patched the parent theme directly, so the change is written down and gets re-checked after every theme update.

Check the rest of the site

After the fix, look for the same pattern elsewhere:

  1. Search the theme for $content[' and similar direct array reads from meta.
  2. Open one page of every template type (post, page, archive, custom type) and check for exactly one non-empty H1.
  3. Turn WP_DEBUG_DISPLAY off on production so warnings go to the log, not to visitors.

Takeaway

A theme that assumes its own data structure will break quietly the moment the site is used in a way the theme author didn't plan for. Guard every read from post meta, and always fall back to the WordPress core value.

I build and maintain small-business websites at Weblevo.

Top comments (1)

Collapse
 
alhiane profile image
Alhiane Lahcen •

Clear example. One thing worth adding: this kind of bug often appears right after a host upgrades PHP. Reading an offset on null was silent before PHP 7.4, became a notice in 7.4 and a warning in 8.0, so a theme that "worked for years" starts printing it overnight.

For the "check every template" step, a small loop saves clicking: for one sample URL per template, curl -s URL | grep -c '<h1' should print 1, and curl -s URL | grep -ci 'warning' should print 0. And since the parent theme was patched directly, turning off auto-updates for it (wp theme auto-updates disable <slug>) means the fix can't be replaced before someone re-checks it.