This will automatically assign the ALT & TITLE attributes to <img> elements that miss those. The value will be the post title ( aka get_the_title() );

 

<?php
function add_alt_tags($content)
{
        global $post;
        preg_match_all('/<img (.*?)\/>/', $content, $images);
        if(!is_null($images))
        {
                foreach($images[1] as $index => $value)
                {
						$new_img = $images[0][$index];
                        if(!preg_match('/alt=/', $value))
                        {
                                $new_img = str_replace('<img', '<img alt="'.$post->post_title.'"', $images[0][$index]);
                        }
                        if(!preg_match('/title=/', $value))
                        {
                                $new_img = str_replace('<img', '<img title="'.$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);

How useful was this post?

Click on a star to rate it!

Average rating / 5. Vote count:

No votes so far! Be the first to rate this post.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?