The WordPress default themes call the image thumbnail like this:
the_post_thumbnail();
By default, the image returned does not have a title
or alt
attribute. (Since WordPress 4.7, the alt attribute is no longer added automatically. It will only have an alt
attribute if you specifically entered “Alt text” while uploading the image, or if you go back to the Media library and enter “Alt text” for the image.)
I find that website traffic from Google search is considerably higher if your images all have title
attribute tags, as well as alt
tags. So, I add the title and alt attributes to post thumbnails with the following function which goes in your functions.php
. The value for the title and alt attributes will be taken from the title of the image, which is the title of the attachment (not the actual post title).
Line 5 adds the title attribute to the featured image. Line 6 adds the alt attribute. You can remove either line if you prefer to omit it.
function isa_add_img_title( $attr, $attachment = null ) { $img_title = trim( strip_tags( $attachment->post_title ) ); $attr['title'] = $img_title; $attr['alt'] = $img_title; return $attr; } add_filter( 'wp_get_attachment_image_attributes','isa_add_img_title', 10, 2 );
If you prefer the title attribute to be taken from the actual post title (instead of the image attachment title), then change line 5 to this:
$attr['title'] = the_title_attribute( 'echo=0' );
Wilton Calderon
January 6th, 2014 at 1:26 pm
i look for this,the_post_thumbnail,thansk love you,and love you work..
PixelRush Studio
April 27th, 2014 at 5:39 am
Hi Isabel.
I just wanted to say a huge thanks to you and your article. I had 1000s of images that were appearing without the title tag. Most of which were either a bug of wordpress or a bug of my template, I think the latter.
Your code assisted me with what to search for and how to update it and I believe I’ve almost fixed all of the issues!
Big help, love your work, thank you!
Byron
Isabel
April 28th, 2014 at 10:53 am
Thank you. Glad to help.
Yasser Hussein
August 3rd, 2014 at 9:51 am
Thank you Isabel – I had been battling with this issue for several hours and your code did the trick for me. I also managed to automatically pull the ALT text from a custom field, so now my post thumbnails are all auto-assigned the correct Alt and Title tags.
Dennis
November 12th, 2014 at 2:05 am
hello,
how can i change the code to use it for ALT-Tag?
And second question: How can i add own text after the automaitcly insert title?
thanks fpr great snippet and help.
Isabel
November 12th, 2014 at 10:45 am
The alt tag is already added by WordPress, but it uses the image title text. If you want the alt tag to use the post title instead, change line 2 to this:
To add your own text after the post title, use this instead:
Replace “YOUR CUSTOM TEXT HERE” with your own text.
Dennis
November 12th, 2014 at 11:59 am
Thank you for helping me out 😉 Great site.
Dennis
November 12th, 2014 at 2:30 pm
Now i see… only for thumbnails, not for nommal insert images in posts. Can i change the snippet to use it on all images?
Celso Bessa
June 9th, 2016 at 7:10 pm
Thanks a lot for sharing it.
It worked like a charm for me. I needed to create a new attribute (data-caption) for a lightbox caption on
www.superlumina.com.br
sites, as title attribute is not a good acessibility practice anymore.Thanks again.
shan
December 27th, 2016 at 9:11 am
HOw to add thumbnail image name as alt tag?, i mean img name as alt tag?
YOu said, this thumbnail image as title name->
Isabel
December 28th, 2016 at 1:05 pm
I updated the code on this page to add the alt attribute. Don’t use lines 8–14 in your example. I hope that helps.
shan
December 29th, 2016 at 2:06 am
Fine, thats cool, may i know how to add the thumbnail image name as alt tag, i mean, get the thumbnail name, not a post tiltle as alt tag, need a image name as alt tag.
and i have added alt tag in every image, but the same image in thumbnail size 150*150, doesnt have alt tag, it was not displayed anywhere, but i tested seo, it displayed some images doesnt have alt tag.
shan
December 29th, 2016 at 3:42 am
May be this will solve the missing alt for every image, replace image title as post title
Waleed Barakat
February 12th, 2017 at 2:50 pm
That’s really awesome solution ,,, Many Thanks for saving my month not just a day 🙂
Waqas Ahmad
March 24th, 2017 at 11:33 am
Thanks for share with us.
http://howtoonlineearn.com/
Chris
April 2nd, 2017 at 9:34 am
Thanks a lot for this great work.
Just test it in WordPress 4.7.3. Works fine with it. All Pictures have title and alt attributes from the name of the File.
Great work!
François
May 30th, 2018 at 12:10 pm
Thanks for the code Isa,
I have been searching for several hours beforehand and your post made me understand a bit more how to do it. Was actually struggling with image attachment when linked to custom taxonomy term. But I also needed the image attributes for my post thumbnails..
We can also get the real “Alt” attribute typed in the media library form doing this :
Manolis
September 17th, 2018 at 6:05 pm
Thank you so much, Isa! You saved me! 😉