Skip to content

Instantly share code, notes, and snippets.

@edheltzel
Last active August 15, 2019 19:14
Show Gist options
  • Save edheltzel/016db63480a4f6da97ff to your computer and use it in GitHub Desktop.
Save edheltzel/016db63480a4f6da97ff to your computer and use it in GitHub Desktop.
Add to your functions.php file to search your content for images without an alt attribute. If they don't have an alt attribute this function will add it in with a default of the post title.
/*-----------------------------------------------------------------------------------*/
/* ADD ALT TAGS TO IMAGE
/* ----------------------------------------------------------------------------------*/
function rdm_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', 'rdm_add_alt_tags', 99999);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment