wp-content » plugins » SEO Friendly WordPress Images with Auto Alt Tag plugin

SEO Friendly WordPress Images with Auto Alt Tag plugin

Here is a simple WordPress plugin that will automatically add the post title as the alt tag to featured images and other images in the post content improving traffic from search engines.

Simply install & activate the and it will automatically add Alt Tag to images in new posts.

image 21 - SEO Friendly WordPress Images with Auto Alt Tag plugin
Alt Tag automatically added to the featured image
<?php
/**
 * Plugin Name: Auto Alt Tag
 * Description: Automatically adds the post title as the alt tag for featured images and images in the post content.
 * Version: 1.0
 * Author: Stefan Pejcic
 */

function auto_alt_tag_for_images($content) {
  if (is_singular()) {
    global $post;
    $post_title = $post->post_title;

    // Find all images in the post content
    preg_match_all('/<img[^>]+>/i', $content, $all_images);

    // Loop through all images and add the post title as the alt tag if the alt tag is empty
    foreach($all_images[0] as $image) {
      if (preg_match('/alt=""/i', $image)) {
        $new_image = str_replace('alt=""', 'alt="' . $post_title . '"', $image);
        $content = str_replace($image, $new_image, $content);
      }
    }

    // Find the featured image and add the post title as the alt tag if the alt tag is empty
    if (has_post_thumbnail()) {
      $thumbnail_id = get_post_thumbnail_id($post->ID);
      $alt = get_post_meta($thumbnail_id, '_wp_attachment_image_alt', true);
      if (empty($alt)) {
        update_post_meta($thumbnail_id, '_wp_attachment_image_alt', $post_title);
      }
    }
  }

  return $content;
}
add_filter('the_content', 'auto_alt_tag_for_images');

Was this post helpful?

Leave a Comment

I enjoy constructive responses and professional comments to my posts, and invite anyone to comment or link to my site.

Recommended