WordPress.org

Make WordPress Core

Opened 7 years ago

Closed 5 years ago

#22413 closed defect (bug) (fixed)

get_the_excerpt() missing a check on $post->post_excerpt

Reported by: johnjamesjacoby Owned by: wonderboymusic
Milestone: 4.0 Priority: low
Severity: minor Version:
Component: Warnings/Notices Keywords: has-patch
Focuses: Cc:
PR Number:

Description

Functions like get_the_title() include isset() or !empty() checks on post object variables, but get_the_excerpt() is missing any such check. This missing check triggers PHP warnings if post_excerpt is not set.

has_excerpt() addresses this for template usage, but nothing addresses this inside get_the_excerpt() itself.

Attached patch adds an isset() check, and correctly passes an empty string if it doesn't.

(Possibly of note: other convenience functions have this same issue -- get_the_guid(), get_the_ID(), etc...)

Attachments (1)

22413.patch (417 bytes) - added by johnjamesjacoby 7 years ago.

Download all attachments as: .zip

Change History (12)

#2 follow-up: @nacin
7 years ago

That's an interesting inconsistency. Under what situation would we have a post object without post_excerpt? Is that still a valid post object? Are these notices we should actually be suppressing, or should developers see them?

With 3.5, any WP_Post object will automatically have post_excerpt = ''.

#3 in reply to: ↑ 2 @johnjamesjacoby
7 years ago

Replying to nacin:

Under what situation would we have a post object without post_excerpt?

Any situation where a plugin decides to unset it. Whether that's right or wrong is debatable, either way, it's possible.

Is that still a valid post object?

It could be? Or it could not be:

global $post; unset( $post->post_excerpt );

...and it's still a post object.

Are these notices we should actually be suppressing, or should developers see them?

We should suppress them where it makes sense to, and here is one of those places, since a post_excerpt isn't operation critical to loading a post (I.E. like a post ID.)

With 3.5, any WP_Post object will automatically have post_excerpt = ''.

Did it not with 3.4? If that's the case, it may already be somewhat of a non-issue specifically related to Jetpack/bbPress.

#4 @wonderboymusic
7 years ago

  • Milestone changed from Awaiting Review to 3.6

#5 @jeherve
7 years ago

  • Cc jeremy+wp@… added

#6 @nacin
6 years ago

I think that if post_excerpt is unset, a notice is deserved. That's exactly a situation where a developer should be notified that they did something wrong, rather than suppressing it.

#7 @aaroncampbell
6 years ago

  • Milestone 3.6 deleted
  • Resolution set to invalid
  • Status changed from new to closed

All WP_Post objects should have a post_excerpt. If a plugin goes out of it's way to unset this, they should get the notice when they try to access it.

#9 @johnjamesjacoby
5 years ago

  • Milestone set to 4.0
  • Resolution invalid deleted
  • Status changed from closed to reopened

Reopening this, as it's akin to #17034.

#10 @wonderboymusic
5 years ago

If there's no $post after get_post(), it should return empty. That would occur if the function is called outside of the loop. The docs don't say it has to be called in the loop.

#11 @wonderboymusic
5 years ago

  • Owner set to wonderboymusic
  • Resolution set to fixed
  • Status changed from reopened to closed

In 28880:

If the get_the_excerpt() is called out of bounds of a global $post, return .

Fixes #22413.

Note: See TracTickets for help on using tickets.