Make WordPress Core

Opened 3 years ago

Last modified 3 years ago

#52389 accepted defect (bug)

Consistently check for non-empty post ID in attachment functions

Reported by: sergeybiryukov's profile SergeyBiryukov Owned by: sergeybiryukov's profile SergeyBiryukov
Milestone: Future Release Priority: normal
Severity: normal Version:
Component: Posts, Post Types Keywords: needs-patch
Focuses: Cc:


Background: #50679, #52196.

As a result of the changes in [49084] and [50039], wp_get_attachment_metadata() conditionally calls get_post() if the attachment ID is not passed:

$attachment_id = (int) $attachment_id;

if ( ! $attachment_id ) {
	$post = get_post();

	if ( ! $post ) {
		return false;

	$attachment_id = $post->ID;

This is not really consistent with other attachment functions, which just always call get_post() unconditionally:

$attachment_id = (int) $attachment_id;

$post = get_post( $attachment_id );

Let's bring some consistency here, there is no reason for these minor differences.

This applies at least to:

  • wp_get_attachment_metadata()
  • wp_get_attachment_url()
  • wp_get_attachment_caption()
  • wp_get_attachment_thumb_file()
  • wp_get_attachment_thumb_url()

Change History (2)

#1 @SergeyBiryukov
3 years ago

  • Owner set to SergeyBiryukov
  • Status changed from new to accepted

#2 @lukecarbis
3 years ago

  • Keywords needs-patch added
  • Milestone changed from 5.7 to Future Release
Note: See TracTickets for help on using tickets.