September 24, 2015

Textual description of firstImageUrl

How To Automatically Replace Missing Alt Tags Of WordPress Images With Post Titles

|
If any of the images in your WordPress does not have an alt or alternate text attribute because you have forgotten to put enter it, here is a quick way to fix it.

This solution will automatically add the post title as alt attribute to the image(s) of that particular blog post.

Replace Missing Alt Tags Of WordPress Images With Post Titles

Add Post Title To Missing Alt Tags WordPress Images


1. Log into your WordPress Dashboard.

2. Click on Appearance.

3. Then click on Theme Functions file (functions.php).

4. Now paste this set of code into the functions.php.

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);
5. Click Update File button.

Warning: Functions.php is very sensitive.

If you paste your code wrongly in this file, you could be locked out of your own site (you cannot log in or log out) and in addition, your site cannot be accessed

Note: If this problem happens to you, then read this post: WordPress Function.php Error: Cannot Log In And Site Disappear.

Related Tutorials:

* Tools To Find Images With Missing Alt Tags
* Plugin To Find And Delete Unused Images In WordPress