2012-03-25 #wordpress

Add Missing Alt Tags To Wordpress Images

Images are important for engagement with your visitors but the problem with images is that they mean nothing to search engines.

The only way search engines can tell what an image is about is by reading the information inside the alt attribute.

If you are using Wordpress you can easily include images on the page by using the media library.

This is where you can select the alt text for the image, but what if you don't have alt text on your images or you forget to enter the alt text? You need a way to automatically populate your image tags with an alt attribute.

Below is a Wordpress snippet to put into your functions.php file to search your content for images which don't have an alt attribute. If they don't have an alt attribute this function will add it in with a default of the post title.

function add_alt_tags($content)
        global $post;
        preg_match_all('/<img (.*?)\/>/', $content, $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);


Reliable uptime monitoring and instant alerts for any website downtime.

  • Uptime Monitoring
  • Performance Monitoring
  • SSL Certificate Alerts
  • Domain Monitoring
  • DNS Checker
  • XML Sitemap Monitoring