Make WordPress Core

Opened 8 years ago

Last modified 5 years ago

#36996 new defect (bug)

get_the_post_thumbnail and the usage of class attribute

Reported by: tomasm's profile TomasM Owned by:
Milestone: Priority: normal
Severity: normal Version: 2.9
Component: Post Thumbnails Keywords:
Focuses: template Cc:


Hi, I noticed that if I use class attribute, it overrides some of the standard classes. So in my example I use header image of custom size:

        echo get_the_post_thumbnail( $post->ID, 'custom-header-image-large', array(
                'id'       => 'featured-image',
                'itemprop' => 'image',
                ) );

In this case WP generates these classes for the header image:

attachment-custom-header-image-large size-custom-header-image-large wp-post-image

If I add attribute 'class' => 'photo u-photo',

        echo get_the_post_thumbnail( $post->ID, 'custom-header-image-large', array(
                'class'    => 'photo u-photo'
                'id'       => 'featured-image',
                'itemprop' => 'image',
                ) );

WP skips some classes and final class list produces only:

photo u-photo wp-post-image

Is it by design, or a possible bug?

Thank you!

Change History (10)

#1 @wido
8 years ago

Your problem is inside the get_attachment_image function where the default arguments ( class includes ) are parsed with the ones you provide.

// wp-includes/media.php:811 ( trunk )
$default_attr = array(
			'src'	=> $src,
			'class'	=> "attachment-$size_class size-$size_class",
			'alt'	=> trim(strip_tags( get_post_meta($attachment_id, '_wp_attachment_image_alt', true) )), // Use Alt field first


// wp-includes/media.php:821 ( trunk )
$attr = wp_parse_args( $attr, $default_attr );

The wp_parse_args use an array_merge where the $default_attr is the first parameter and the second is $attr, so, the second parameter override the first one.

I don't know if this is a bug or not, but you can use the 'wp_get_attachment_image_attributes' filter which will pass $attr, $attachment and $size to add again the classes.

Version 0, edited 8 years ago by wido (next)

#2 @TomasM
8 years ago

Thank you for the explanation! I'm not PHP pro, so if it is possible may I ask for the rough example on how to implement your proposed solution?

Thank you!

#3 @wido
8 years ago

@TomasM I made this little snippet that add again the default classes.

function _add_again_default_attach_image_class_attr( $attr, $attach, $size ) {

	// The class to test.
	$classes = 'attachment-' . $size . ' size-' . $size;

	// Add the wp classes if not founds.
	if ( isset( $attr['class'] ) && false === strpos( $attr['class'], $classes ) ) {
		$attr['class'] .= ' ' . $classes;

	return $attr;

add_filter( 'wp_get_attachment_image_attributes', '_add_again_default_attach_image_class_attr', 10, 3 );

If this will considered a bug, we can do the same thing in code, just for example. Store the $attr parameter into $user_attr before the wp_parse_args and then check if the $user_attr has a key 'class', that means the $default classes have been overwrite, if so, append the default classes to the new $attr class string.

Last edited 8 years ago by wido (previous) (diff)

#4 @TomasM
8 years ago

Thank you! I will try it out.

#5 @TomasM
8 years ago

  • Keywords has-unit-tests dev-feedback added

I tested the code and it works perfectly - thank you!

It works for parent theme as well as for a child theme.

To me it looks like a bug, because looking at other similar WP functions, from my limited experience usually classes would be added, leaving default classes intact.

I hope that this bug will be fixed in the future release.

Thank you again!

#6 @TomasM
8 years ago

  • Focuses template added

@wido I would like to ask you one related question. One user reported to me this error:

Not sure if it is just my setup (Ubuntu edge - PHP 7.0.8-0ubuntu0.16.04.3 (cli) ) but when I add a featured image to a post I get:
Notice: Array to string conversion in /var/www/html/wordpress-network/wp-content/themes/tiny-framework/functions.php on line 811 and the same again when I click on an image which has been added to a gallery page using the built in Wordpress create a gallery function. I see that in image.php you have:

$image_size = apply_filters( 'tinyframework_attachment_size', array( 960, 960 ) );

and in functions.php line 811 you have:

$classes = 'attachment-' . $size . ' size-' . $size;

I changed it to:

$classes = 'attachment-' . $size[0] . ' size-' . $size[1];

and poof! No more error. (End of his message)

Personally I have never seen that error and nobody else reported it, but if the solution is a good improvement, I will add it to my theme. I tested it on my server and it didn't cause any issues.

Last edited 8 years ago by TomasM (previous) (diff)

#7 @grapplerulrich
8 years ago

  • Component changed from General to Media
  • Keywords has-unit-tests removed
  • Version changed from 4.5.2 to 2.9

Correcting some of the ticket data. As the code was introduced in WP 2.9 we will need to think about backward compatibility too.

@TomasM "unit-tests" is PHP code that checks that the code that is written works as expected. As there is no patch the keyword does not apply.

In the future please make sure to select a component that is most related, that way it will get looked at by the right person.

Your second question is more of a support question then a issue with core. You will get a better response on a support forum. You may also get some tips from your local meetup

#8 @TomasM
8 years ago

Thank you! I'm sorry about the unit test, I thought I can add this, because I tested the code and it appears to work :)

#9 @grapplerulrich
8 years ago

  • Component changed from Media to Post Thumbnails
  • Keywords dev-feedback removed

#10 @TomasM
8 years ago

Looks like my question:

was solved by @bcworkz at

if ( is_array( $size ) ) {
   $size = "$size[0]x$size[1]";
   $classes = 'attachment-' . $size . ' size-' . $size;
} else {
   $classes = 'attachment-' . $size . ' size-' . $size;
Last edited 8 years ago by TomasM (previous) (diff)
Note: See TracTickets for help on using tickets.