Skip to content

Instantly share code, notes, and snippets.

@adamdoe
Created July 21, 2016 20:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamdoe/815a94c4c845c5edfacd067896af72c9 to your computer and use it in GitHub Desktop.
Save adamdoe/815a94c4c845c5edfacd067896af72c9 to your computer and use it in GitHub Desktop.
function add_alt_tags($content)
{
global $post;
preg_match_all('/<img (.*?)\/>/', $content, $images);
if(!is_null($images))
{
foreach($images[1] as $index => $value)
{
if(!preg_match('/alt=/', $value))
{
$new_img = str_replace('<img', '<img alt="'.$post->post_title.'"', $images[0][$index]);
$content = str_replace($images[0][$index], $new_img, $content);
}
}
}
return $content;
}
add_filter('the_content', 'add_alt_tags', 99999);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment