Making WordPress.org

Opened 4 months ago

Closed 4 months ago

#8105 closed defect (bug) (invalid)

author-template.php generates PHP Warning with PHP 8.3

Reported by: cornwell's profile Cornwell Owned by:
Milestone: Priority: normal
Component: General Keywords:
Cc:

Description

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 93

This in WordPress release 6.8.3. Line 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.

I appreciate the root cause may be that the function is called outside the loop, but this function should handle this. My suggested code achieves this.

Change History (1)

#1 @Otto42
4 months ago

  • Resolution set to invalid
  • Status changed from new to closed

If this is a core issue, then you should post on core.trac.wordpress.org.

The meta trac is for tracking issues with the WordPress.org website itself, or its related properties.

Note: See TracTickets for help on using tickets.