WordPress.org

Make WordPress Core

Opened 9 months ago

Last modified 8 months ago

#52389 accepted defect (bug)

Consistently check for non-empty post ID in attachment functions

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

Description

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
9 months ago

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

#2 @lukecarbis
8 months ago

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