Make WordPress Core

Opened 13 years ago

Closed 12 years ago

Last modified 12 years ago

#18491 closed defect (bug) (fixed)

Calling wp_get_attachment_link with a non-existent ID throws PHP notice

Reported by: jayjdk's profile Jayjdk Owned by: ryan's profile ryan
Milestone: 3.3 Priority: normal
Severity: normal Version:
Component: Warnings/Notices Keywords: has-patch
Focuses: Cc:

Description

If you're are using the wp_get_attachment_link() function with a non-existent attachment ID, you'll get a notice.

Example:
<?php echo wp_get_attachment_link( 9 ); ?>

gives Notice: Trying to get property of non-object in C:\wamp\www\theme\wp-includes\post-template.php on line 1155

As far I can see, the code causing it is this (wp-includes\post-template.php on line 1155)

$_post = & get_post( $id );

if ( ('attachment' != $_post->post_type) || !$url = wp_get_attachment_url($_post->ID) )
	return __('Missing Attachment');

The $_post variable will be null in this case.

Attachments (2)

18491.patch (540 bytes) - added by SergeyBiryukov 13 years ago.
18491_2.patch (1.8 KB) - added by Jayjdk 12 years ago.
Updated the function to follow the WordPress Coding Standards

Download all attachments as: .zip

Change History (7)

#1 @SergeyBiryukov
13 years ago

  • Keywords has-patch added; needs-patch removed
  • Milestone changed from Awaiting Review to 3.3

#2 @Jayjdk
13 years ago

Nice! Just to confirm; the patch from Sergey fixes the error

@Jayjdk
12 years ago

Updated the function to follow the WordPress Coding Standards

#3 @ethitter
12 years ago

  • Cc ehitter@… added

Tested 18491_2.patch and it works for me.

#4 @ryan
12 years ago

  • Owner set to ryan
  • Resolution set to fixed
  • Status changed from new to closed

In [19210]:

Avoid warning when non-existent ID pass to wp_get_attachment_link(). Tidy formatting of wp_get_attachment_link(). Props SergeyBiryukov, Jayjdk. fixes #18491

#5 @nacin
12 years ago

In [19212]:

Simplify logic in wp_get_attachment_link(). see #18491.

Note: See TracTickets for help on using tickets.