0

I am trying to use wp_get_attachment_image() and set a different title attribute from what is stored in the media library for that image.

echo wp_get_attachment_image($image_id, 'large', false, array ('title' => 'My Custom Title'));

However I cannot get it to override the title attribute at all; I can override alt, id, class etc but not title.

Am I doing something wrong or is this not possible?

1

In a relatively fresh install I can't replicate the issue. When I post your code with my own image ID I get the title="My Custom Title" attribute.

It's important to note however that the "Title" of an image in the media library is not output as a title attribute in the standard output of wp_get_attachment_image(). The "Title" is just the name of the listing in the media library. WordPress does not output a title attribute automatically, I think because it's not considered a benefit for accessibility. By default it will only output the value of "Alt Text" as an alt attribute.

You should be able to set the attribute in wp_get_attachment_image() the way you have, however. If there is a title attribute being added without your intervention then a plugin or theme must be adding it with the wp_get_attachment_image_attributes filter. This filter is run after your attributes are assigned, so if there is a value you cannot overwrite in the attributes argument, then this would explain why.

  • That's exactly what the problem was. Did a search in the project and found the filter had been added at some point. Thanks! – feroxy Feb 23 '18 at 18:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.