#64104 closed defect (bug) (fixed)
author-template.php generates PHP Warning with PHP 8.3
| Reported by: |
|
Owned by: |
|
|---|---|---|---|
| Milestone: | 6.9 | Priority: | normal |
| Severity: | normal | Version: | 2.8 |
| Component: | Posts, Post Types | Keywords: | has-patch has-unit-tests |
| Focuses: | 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.
Attachments (2)
Change History (15)
#2
@
5 months ago
I'm still new to this process. Do I need to do anything further to get this change into the WordPress core, or will others now take it forward?
This ticket was mentioned in Slack in #core by amykamala. View the logs.
5 months ago
#4
@
5 months ago
- Milestone changed from Awaiting Review to 6.9
- Owner set to westonruter
- Status changed from new to reviewing
@Cornwell Thanks for reporting! I'll review the issue and the patches to fix in 6.9.
#6
@
5 months ago
@jdahir0789 Your patch 64104.patch fixes the issue.
@dhruvang21 I like the idea of your patch fix-64104.patch in that it adds the $post_id parameter similar to get_the_date(), get_the_modified_time(), get_post_modified_time(), get_the_modified_author() function API much more similar to other similar functions, like get_the_time(), get_post_time(), get_post_datetime(), and get_post_timestamp(). However, your patch doesn't actually fix the issue. In this line here:
<?php $post_id = $post_id ? $post_id : get_post()->ID;
If there is no global $post then get_post() will return null and the same error will occur. However, this can be tweaked
<?php $post = get_post( $post ); if ( ! $post ) { return; } $post_id = $post->ID;
I'll make that change.
This ticket was mentioned in PR #10400 on WordPress/wordpress-develop by @westonruter.
5 months ago
#7
- Keywords has-patch has-unit-tests added
Trac ticket: https://core.trac.wordpress.org/ticket/64104
@westonruter commented on PR #10400:
5 months ago
#9
It might be worth noting this only applies to the classic editor, since updating a post in the block editor doesn't set the
_edit_lastvalue. Maybe that's clear/expected since it's a PHP template function?
@mindctrl woah, what? Is that tracked anywhere as a bug? That seems like an oversight.
@westonruter commented on PR #10400:
5 months ago
#10
Reported way back here: https://github.com/WordPress/gutenberg/issues/31501.
related: #63263