Jump to content

Tweaking auto alt tag generation in Wordpress


popokolok

Recommended Posts

Hello,

I tried many WP plugins to create automatic alt tags site-wide. None of the available plugins seem to work (I Tried SEO Friendly Images, Wordpress SEO Images, WP Image SEO) even with the default theme... Any other plugins anyone can suggest, please?

Anyway, I found this code that's supposed to go into the functions.php of the theme, but it doesn't seem to work... It's supposed to check for images without an alt tag and fill in the post title - can anyone here please help me with making this code to change ALL alt tags by putting the post title in them and making this code work in the first place..?

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);

Thank you very much!

Edited by popokolok
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.