Make WordPress Core

Opened 9 years ago

Closed 9 years ago

#33277 closed defect (bug) (fixed)

Misleading comment for wp_get_attachment_link function

Reported by: several27's profile several27 Owned by: wonderboymusic's profile wonderboymusic
Milestone: 4.4 Priority: normal
Severity: normal Version: 4.2.3
Component: Media Keywords: has-patch 4.4-early commit
Focuses: Cc:

Description

The comment for function wp_get_attachment_link inside wp-includes/post-template.php says that the first argument can be either int or WP_Post object, but that is not true.
This is the first line inside this function:

$id = intval($id);

This line will cause the error: Object of class WP_Post could not be converted to int.

Please forgive me any mistakes this is my first ticket :)

Attachments (3)

patch.diff (1.3 KB) - added by several27 9 years ago.
patch.2.diff (809 bytes) - added by several27 9 years ago.
33277.diff (1.2 KB) - added by DrewAPicture 9 years ago.
+ changelog entry

Download all attachments as: .zip

Change History (15)

#1 follow-up: @SergeyBiryukov
9 years ago

  • Component changed from Comments to Media
  • Keywords needs-patch added

Hi @several27, welcome to Trac and thanks for the report!

Introduced in [27473]. We could probably just remove the intval() and let get_post() handle the value.

Would you like to try submitting a patch?

#2 in reply to: ↑ 1 ; follow-up: @several27
9 years ago

Replying to SergeyBiryukov:

Hi @several27, welcome to Trac and thanks for the report!

Introduced in [27473]. We could probably just remove the intval() and let get_post() handle the value.

Would you like to try submitting a patch?

Hi and thank you for the incredibly quick answer :)

I just verified it and just removing intval() won't work, line 1546: will have to be changed also:

$link_text = wp_get_attachment_image( $id, $size, $icon, $attr );

to

$link_text = wp_get_attachment_image( $_post->ID, $size, $icon, $attr );

And after that it should work (at least is working for me).

So how should I submit a patch?

Last edited 9 years ago by several27 (previous) (diff)

@several27
9 years ago

@several27
9 years ago

#4 @several27
9 years ago

I just uploaded patch, the patch.2.diff should be the correct one.

#5 @SergeyBiryukov
9 years ago

  • Keywords has-patch 4.4-early added; needs-patch removed
  • Milestone changed from Awaiting Review to Future Release

#6 follow-up: @MikeHansenMe
9 years ago

Just dug into this a bit and it looks like it has never supported a post object. The code was introduced in https://github.com/WordPress/WordPress/commit/0456afb16d10ad4ccc0d1a5a9b979f35ce7135e0 it looks like the previous function(get_the_attachment_link) that was deprecated also forced int. So is this just a problem with the function doc implying it can be a post object?

Version 0, edited 9 years ago by MikeHansenMe (next)

#7 in reply to: ↑ 6 ; follow-up: @knutsp
9 years ago

Replying to MikeHansenMe:

So is this just a problem with the function doc stating it can be a post object?

Probably, and that's a bug. This patch solves it, and at the same time, it's an enhancement.

#8 in reply to: ↑ 7 @DrewAPicture
9 years ago

Replying to knutsp:

Replying to MikeHansenMe:

So is this just a problem with the function doc stating it can be a post object?

Probably, and that's a bug. This patch solves it, and at the same time, it's an enhancement.

So, realistically, we should add a changelog entry mentioning that it now actually supports a WP_Post object.

#9 @wonderboymusic
9 years ago

  • Milestone changed from Future Release to 4.4

#10 @wonderboymusic
9 years ago

  • Owner set to DrewAPicture
  • Status changed from new to assigned

@DrewAPicture
9 years ago

+ changelog entry

#11 @DrewAPicture
9 years ago

  • Focuses docs removed
  • Keywords commit added
  • Owner DrewAPicture deleted

33277.diff adds the changelog entry and regenerates the patch from root.

#12 @wonderboymusic
9 years ago

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

In 33669:

In wp_get_attachment_link(), accept an id or WP_Post as the first parameter.

Props several27, DrewAPicture.
Fixes #33277.

Note: See TracTickets for help on using tickets.