Skip to content

Instantly share code, notes, and snippets.

@RadGH
Last active June 14, 2018 06:03
Show Gist options
  • Save RadGH/8600eef8bd03575b0fa76b66faff8389 to your computer and use it in GitHub Desktop.
Save RadGH/8600eef8bd03575b0fa76b66faff8389 to your computer and use it in GitHub Desktop.
Fill in alt text for images in post content
<?php
/*
If alt text isn't included in image attributes from the_content, it is added in.
And, if alt text is not given, image caption or title will be used instead.
BEFORE:
<img src="http://example.org/image.jpg" alt="This alt text is entered by hand!" width="1500" height="996" />
<img src="http://example.org/image.jpg" alt="" width="1500" height="996" />
<img src="http://example.org/image.jpg" alt='' width="1500" height="996" />
<img src="http://example.org/image.jpg" alt width="1500" height="996" />
<img src="http://example.org/image.jpg" width="1500" height="996" />
AFTER:
<img src="http://example.org/image.jpg" alt="This alt text is entered by hand!" width="1500" height="996" />
<img src="http://example.org/image.jpg" alt="Picture of a cute dog" width="1500" height="996" />
<img src="http://example.org/image.jpg" alt="Picture of a cute dog" width="1500" height="996" />
<img src="http://example.org/image.jpg" alt="Picture of a cute dog" width="1500" height="996" />
<img alt="Picture of a cute dog" src="http://example.org/image.jpg" width="1500" height="996" />
*/
/**
* Fill alt tags for images inside post content automatically, whenever the alt text is blank in the code but exists as attachment metadata.
*
* Note: Requires external function, rs_reverse_media_id_lookup()
* Get it here: https://gist.github.com/RadGH/e15cbb190474a1b7b22a8d5eb99a25c8
*
* @see https://gist.github.com/RadGH/8600eef8bd03575b0fa76b66faff8389
*
* @param $content
*
* @return mixed
*/
function rs_fill_missing_alt_tags( $content ) {
if ( preg_match_all('/<img (.+?)\/?>/', $content, $images) ) {
foreach( $images[1] as $index => $value ) {
// Get the attachment id from the full image URL
$attachment_id = rs_reverse_media_id_lookup( $images[0][$index] );
if ( !$attachment_id ) continue; // External image?
// Get the image alt, or caption, or title... otherwise skip the image.
$alt = get_post_meta( $attachment_id, '_wp_attachment_image_alt', true );
if ( !$alt ) {
$p = get_post( $attachment_id );
if ( $p->post_excerpt ) $alt = $p->post_excerpt;
else if ( $p->post_title ) $alt = $p->post_title;
else continue;
}
// Find alt tags that do not have content, such as:
// <img alt src="test.jpg">
// <img alt="" src="test.jpg">
// <img alt='' src="test.jpg">
// SEE MORE: https://regexr.com/3kj2f
if ( preg_match( '/\salt(=([\'"])\2|(?!=))/', $value, $found) ) {
$new_img = str_replace( $found[0], ' alt="'. esc_attr(preg_quote($alt)) .'"', $images[0][$index] );
$content = str_replace( $images[0][$index], $new_img, $content );
}
// Or if the image has no alt tag, add one in.
else if ( stripos($value, 'alt=') === false ) {
$new_img = str_replace( $value, 'alt="'. esc_attr(preg_quote($alt)) .'" ' . $value, $images[0][$index] );
$content = str_replace( $images[0][$index], $new_img, $content );
}
}
}
return $content;
}
add_filter( 'the_content', 'rs_fill_missing_alt_tags', 20 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment