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 the co-founder and lead developer at CultivateWP, a WordPress agency focusing on high performance sites for web publishers.

About Me
Ready to upgrade your website?

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

Let's Talk