[wp-trac] [WordPress Trac] #29443: wp_get_attachment_image 4st. parameter $attr should be reference &$attr

WordPress Trac noreply at wordpress.org
Sun Aug 31 21:42:52 UTC 2014


#29443: wp_get_attachment_image 4st. parameter $attr should be reference &$attr
--------------------------+------------------------------
 Reporter:  heddesheimer  |       Owner:
     Type:  defect (bug)  |      Status:  new
 Priority:  normal        |   Milestone:  Awaiting Review
Component:  Media         |     Version:  3.9.2
 Severity:  normal        |  Resolution:
 Keywords:  close         |     Focuses:
--------------------------+------------------------------
Changes (by SergeyBiryukov):

 * keywords:  media, thumbnail, wp_get_attachment_image, attr => close


Old description:

> When I tried to hook in to wp_get_attachment_image() in media.php to
> override the html of the post_thumbnail, I found that the $attr variable
> never passed any values to the hooked callback function.
>
> This makes sense, as arrays are created as copies in PHP functions,
> therefore the 4st. parameter must be &$attr instead of just $attr. When I
> changes that in the function, my $attr values all gets passed fine to the
> intercepting function that uses the hook.
>
> To reproduce the problem:
> add a filter like this to your plugin/theme:
>
>     add_filter( 'post_thumbnail_html', 'my_post_thumbnail_html', 10, 5 );
>
> Then try to show the passed attributes in the callback function like
> this:
>
>     function my_post_thumbnail_html($html, $post_id, $post_thumbnail_id,
> $size, $attr)
>     {
>         print_r($attr); exit;
>     }
>
> You will see that nothing is displayed. When you change the 4st.
> parameter of wp_get_attachment_image in wp-includes/media.php to &$attr,
> it will show the attributes like this:
>
>     Array ( [src] => http://testing.local/wordpress296/wp-
> content/uploads/2014/08/about-image.jpg [class] => attachment-450x300 wp-
> post-image [alt] => alternative text )
>
> which is the expected result for the filter.

New description:

 When I tried to hook in to wp_get_attachment_image() in media.php to
 override the html of the post_thumbnail, I found that the $attr variable
 never passed any values to the hooked callback function.

 This makes sense, as arrays are created as copies in PHP functions,
 therefore the 4st. parameter must be &$attr instead of just $attr. When I
 changes that in the function, my $attr values all gets passed fine to the
 intercepting function that uses the hook.

 To reproduce the problem:
 add a filter like this to your plugin/theme:
 {{{
     add_filter( 'post_thumbnail_html', 'my_post_thumbnail_html', 10, 5 );
 }}}
 Then try to show the passed attributes in the callback function like this:
 {{{
     function my_post_thumbnail_html($html, $post_id, $post_thumbnail_id,
 $size, $attr)
     {
         print_r($attr); exit;
     }
 }}}
 You will see that nothing is displayed. When you change the 4st. parameter
 of wp_get_attachment_image in wp-includes/media.php to &$attr, it will
 show the attributes like this:
 {{{
     Array ( [src] => http://testing.local/wordpress296/wp-
 content/uploads/2014/08/about-image.jpg [class] => attachment-450x300 wp-
 post-image [alt] => alternative text )
 }}}
 which is the expected result for the filter.

--

Comment:

 `get_the_post_thumbnail()` accepts an `$attr` parameter and passes the
 original, unmodified value to the [source:tags/3.9.2/src/wp-includes/post-
 thumbnail-template.php#L130 post_thumbnail_html] filter.

 If you need access to the values calculated by
 `wp_get_attachment_image()`, use [source:tags/3.9.2/src/wp-
 includes/media.php#L717 wp_get_attachment_image_attributes] filter
 instead.

--
Ticket URL: <https://core.trac.wordpress.org/ticket/29443#comment:1>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list