Image Attribute Fallback

<?php
/**
* Image Attribute Fallback
*
*/
function be_image_attr_fallback( $attr, $attachment ) {
if( empty( $attachment->post_parent ) )
return $attr;
$title = get_the_title( $attachment->post_parent );
if( empty( $attr['alt'] ) )
$attr['alt'] = $title;
if( empty( $attr['title'] ) )
$attr['title'] = $title;
return $attr;
}
add_filter( 'wp_get_attachment_image_attributes', 'be_image_attr_fallback', 10, 2 );
view raw functions.php hosted with ❤ by GitHub

If you want an alt and title tag for every image, this uses the title of the page to which you uploaded the image as the fallback.

Bill Erickson

Bill Erickson is a freelance WordPress developer and a contributing developer to the Genesis framework. For the past 14 years he has worked with attorneys, publishers, corporations, and non-profits, building custom websites tailored to their needs and goals.

Ready to upgrade your website?

I build custom WordPress websites that look great and are easy to manage.

Let's Talk