Archived topic
A PHP 8.0 peculiarity
4 replies · Started by Relja on September 15, 2021
Setup:
Fresh website, with GeneratePress.
Problem:
It all works fine with PHP 7.4
It also works fine with PHP 8.0, but I get this error in the WP root directory (on the server):
[15-Sep-2021 19:59:44 UTC] PHP Warning: Undefined variable $custom_content in /home/blablabla/public_html/wp-content/themes/generatepress-child/functions.php on line 26
Here's what's in line 26 (the bolded line is 26):
// BEGIN LAST-EDITED-updated ISPISIVANJE
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');
// THE PROBLEMATIC LINE 26:
$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-EDITED-updated ISPISIVANJE
The function does its job with no problems, even when that error gets reported in the root directory.
When I install and activate a plugin called "Subscribe To Comments Reloaded," the error code goes away.
Likewise, if I don't change anything, just set the PHP back to 7.4 - the problem goes away also.
I've checked everything else - PHP extensions, configuration, asked the provider to confirm the 8.0 is running OK.
I use the above-noted plugin on several websites - and they had no problems with the error.log appearing.
That's how I finally figured out the... strange situation.
The problem happens with GeneratePress and with GeneratePress premium.
It doesn't happen with TwentyTwentyone.
When all else fails, read the docs... :)
I've managed to sort it out. Still have no idea why that code shows error (while it still works) with PHP 8.0, but I've completely removed it, and placed this in the child-theme's 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 */
It works. No "Updated" info on pages, only on posts, but that makes more sense IMO, it's probably an even better solution. :)
Marking this as resolved.
Hi there,
Glad you found a solution.
Just a note that the block element post meta makes it easy for things like these as well.
More info here:
https://docs.generatepress.com/article/block-element-post-meta-template/
https://docs.generatepress.com/article/dynamic-data/
In case it helps, here's the full analysis report - what I tested, how I found the problem, how I solved it - with a few dilemmas still left unsolved (but it all works fine now):
https://io.bikegremlin.com/22270/strange-wordpress-php-8-0-error/
Thanks for sharing!