author-template.php generates PHP Warning with PHP 8.3
-
I am seeing the following warning message in my php error log:
PHP Warning: Attempt to read property “ID” on null in /home/[userid]/public_html/wp-includes/author-template.php on line 93Line 93 is
$last_id = get_post_meta( get_post()->ID, '_edit_last', true );It occurs in this function:
/**
* Retrieves the author who last edited the current post.
*
* @since 2.8.0
*
* @return string|void The author's display name, empty string if unknown.
*/
function get_the_modified_author() {
$last_id = get_post_meta( get_post()->ID, '_edit_last', true );
if ( $last_id ) {
$last_user = get_userdata( $last_id );
/**
* Filters the display name of the author who last edited the current post.
*
* @since 2.8.0
*
* @param string $display_name The author's display name, empty string if unknown.
*/
return apply_filters( 'the_modified_author', $last_user ? $last_user->display_name : '' );
}
}I think this code relies on line 93 returning a null value if the ID has not been set (as would probably be the case if the function were called outside the loop). However, php at current releases (mine is at 8.3) generates a warning message and this is filling my logs.
My solution has been to replace line 93 with
if ( isset(get_post()->ID) ) {
$last_id = get_post_meta( get_post()->ID, '_edit_last', true );
} else {
$last_id = '';
}Since implementing this change no errors have been flagged.
Is this something I might suggest as a fix?
The topic ‘author-template.php generates PHP Warning with PHP 8.3’ is closed to new replies.