Start » WordPress » Troubleshooting » Strange WordPress PHP 8.0 error

Strange WordPress PHP 8.0 error

In this article, I’ll describe a strange WordPress PHP 8.0 related error that I haven’t seen before – and I’ve been using WordPress for about seven years.


Table Of Contents (T.O.C.):

  1. PHP 8.0
  2. WordPress PHP 8.0 error
  3. Figuring out the problem cause
  4. Solving the problem
  5. Dilemma


1. PHP 8.0

Because of the spirit of the current time (zeitgeist) in software development, I’m cautious when using new software versions and updating in general.

My websites have been happily running on PHP 7.4 for a few years now. They all use the same theme and a very similar set of plugins. However, no one makes any money when the old software just works – it has to be “upgraded.” Alas, PHP 7.4 is nearing its end of life:

PHP versions support (EOL - End Of Life) timeline
PHP versions support (EOL – End Of Life) timeline – source: php.net
Picture 1

WordPress core and many themes and plugins are still far from thoroughly tested with PHP 8.0, while the clock is relentlessly ticking – a few months left until PHP 7.4 is entering its retirement stage.

It’s about time I tested and moved my websites to PHP 8.0, solving any potential problems that occur. So I first tested using website clones in a staging environment – all good. OK, I can try switching one production website at a time to the new PHP version. For extra precaution, I decided to start with my blog – no one reads that (Mostly Useless™ ). 🙂

It all went smoothly and I switched all the websites without any problems. I’m quite cautious when choosing WordPress themes and plugins, but I was still pleasantly surprised! Now comes the scene where I’m smiling and running in a flower field, while in the background, that eery horror-film music gets louder…

– T.O.C. –


2. WordPress PHP 8.0 error

I decided to finally make the BikeGremlin ODEI™ website more decent – to switch it from static HTML to WordPress, polish the visuals, user interface and improve the search so it covers all of my projects.

Because I don’t plan a Q & A section on that very site, so I didn’t installthe Subscribe To Comments Reloaded plugin (wp.org link).

The rest of the setup was the same as for my other websites – theme, child theme custom code, other plugins etc.

What could possibly go wrong?! 🙂

The website worked nicely, but I checked error logs on the hosting server just in case (error log location for cPanel and DirectAdmin control panels).

WordPress home directory php error log
WordPress home directory PHP error log
Picture 2

Opening the log showed this error numerous times (once every time a page was opened):

[15-Sep-2021 19:59:44 UTC] PHP Warning: Undefined variable $custom_content in /home/bikegremlin/public_html/wp-content/themes/generatepress-child/functions.php on line 26

All right, the problem is with my WordPress child theme custom code, on line 26 (bolded):

// BEGIN Last-updated display

function wpb_last_updated_date( $content ) {
    $u_time = get_the_time('U'); 
    $u_modified_time = get_the_modified_time('U');
    if ($u_modified_time >= $u_time + 86400) { 
        $updated_date = get_the_modified_time('d/m/Y');
        $custom_content .= '<p class="last-updated">Updated: '. $updated_date .   '.  </p>';
    }
    $custom_content .= $content;
    return $custom_content;
}
add_filter( 'the_content', 'wpb_last_updated_date' );

// END Last-updated display

But it all works great on the frontend – even that function does its job and shows the results properly?!?

To make it even more confusing – the same function shows no errors on the other websites!

– T.O.C. –


3. Figuring out the problem cause

I tried a fresh WordPress install, with the same theme and plugins on a new cPanel account – got the same error.

I double-checked if everything on the problematic website is configured exactly as it is on the websites that don’t report the error – but I didn’t install the comments subscription plugin.

I’ve had plugins cause problems when they are installed, but I’ve never had problems caused by not installing a plugin. My custom code doesn’t use that plugin, and there are no errors with PHP 7.4 (even without that plugin installed).

See how (and why) I missed noticing this?

What I did notice is that there’s no error with PHP 7.4, and there’s also no error with the Twenty Twenty-One theme (use it to not forget which year it is 🙂 )correction, I hadn’t added the problematic code to the Twenty Twenty-One child theme.

So I did everything else, as a classic amateur:

  • I asked the hosting provider to check if PHP 8.0 is properly installed and if it’s possible for it to cause problems only on certain cPanel accounts (and work on others). They checked and confirmed it’s all good on their end.
    I’m using HostMantis hosting and must say their tech support was fast and did their job well. As I’ve confirmed later, it wasn’t a problem on their end – it was my bad code.
  • Started a topic on the GeneratePress support forum.
  • Bothered the good folks on the LowEndSpirit forum.

It was still a Catch 22 situation – had to figure it out myself. With a lot of good tips, hints and help, but still no solution.

“We are all agreed that your theory is crazy. The question that divides us is whether it is crazy enough to have a chance of being correct.”

– Niels Bohr in a reply to Wolfgang Pauli

I decide to make the new site be a 1-on-1 copy of the sites that show no errors, adding all the “missing” plugins – even though they are needless for the new site. After a few hours, I figure out that installing the Subscribe To Comments Reloaded plugin (wp.org link). With it there is no error, even with GeneratePress theme and PHP 8.0.

– T.O.C. –


4. Solving the problem

What is the main difference between WordPress plugins, and bicycle wheel spokes?
– The fewer, the better!

Installing a needless WordPress plugin is a road to ruin (no, I don’t mean the Ramones album! 🙂 ).

With that in mind, these were my design constraints:

  • The website must work with PHP 8.0.
  • I want to use the GeneratePress theme, because it is among the best ones.
  • No needless plugins can be installed!

One of the reasons why GeneratePress is the best WordPress theme is its phenomenal documentation (link to GP docs). 🙂

I’m ditching my custom function. As johnhk said on LES forum:

The issue is in PHP 8.0, usage became more strict compared to 7.4. So, technically “improper” assignment like what you were doing throws a warning now.

– johnk

And I’m adding the following code to my child theme style.css:

/* BEGIN updated date showing only */

.posted-on .updated {
    display: inline-block;
}

.posted-on .updated + .entry-date {
    display: none;
}

.posted-on .updated:before {
    content: "Updated ";
}

/* END updated date showing only */

And I think that’s solved – Proper Job™ as I call it. 🙂

A corrected code for my original function would be this (missing line defining the variable is bolded):

function wpb_last_updated_date( $content ) {
    $u_time = get_the_time('U'); 
    $u_modified_time = get_the_modified_time('U');
    $custom_content = '';
    if ($u_modified_time >= $u_time + 86400) { 
        $updated_date = get_the_modified_time('d/m/Y');
        $custom_content .= '<p class="last-updated">Updated: '. $updated_date .   '.  </p>';
    }
    $custom_content .= $content;
    return $custom_content;
}
add_filter( 'the_content', 'wpb_last_updated_date' );

This solution doesn’t show the updated date on pages, only on posts. This makes more sense than my previous choice – since posts (articles) is where the updated time matters.

– T.O.C. –


5. Dilemma

The problem’s been solved (for now at least), but I’m still wondering:

  1. If the problem cause was my bad code and its incompatibility with PHP 8.0 (which wouldn’t amaze me, since I’m a grease-monkey, not a programmer), why does the theme Twenty Twenty-One don’t generate the error report?
    – The answer is that I hadn’t added the problematic code to the Twenty Twenty-One child theme.
  2. How and why does the Subscribe To Comments Reloaded plugin (wp.org link) eliminate the error log reporting?

It seems to me that in the software industry, and most other industries, there’s more and more marketing, and not enough testing and documenting.

For my use case, WordPress is still the least bad solution, but I’m not a fan of frequent “updates & upgrades” of everything. In any case, I still don’t fix my friends’ bicycle brakes without a test ride afterwards.

– T.O.C. –

Leave a Comment

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Google chosen content: