WordPress optimization to add ALT and title attributes to the article image

In WordPress optimization. Image ALT and image title optimization is a very important SEO optimization, but if there are too many images in the article, it will be very troublesome to manually add alt attribute and image title attribute to each image, especially when there are too many images.

Of course, there are WordPress plugins that can automatically add ALT and title to images, but you don’t like to use plugins, so today you share a WordPress tutorial that uses plugin free pure code to automatically add ALT and title to images.

The method is very simple, copy the above code to the functions.php File is enough

//Auto add to article imagealtandtitleinformation
add_filter('the_content', 'imagesalt');
function imagesalt($content) {
       global $post;
       $pattern ="/<a(.*?)href=('|")(.*?).(bmp|gif|jpeg|jpg|png)('|")(.*?)>/i";
       $replacement = '<a$1href=$2$3.$4$5 alt="'.$post->post_title.'" title="'.$post->post_title.'"$6>';
       $content = preg_replace($pattern, $replacement, $content);
       return $content;

After adding the code, refresh the article to see the properties of the image. Are there more ALT and tile properties?

Related Article
Two methods of automatically renaming uploaded files in WordPress

When we edit and publish articles, we often upload some pictures, audio and other files. Interested webmasters will find that Read more

WordPress tutorial: adding author filtering options to the background article list

When there are multiple users publishing articles in our site, in order to facilitate the administrator to view the article Read more

WordPress displays the registration time of the login user in the frontend

When using WordPress to build a website, some users want to display the user registration time in the frontend, but Read more

WordPress adds the number of SQL queries at the bottom

An excellent woodpress theme is definitely that the less SQL consulting is used, the better. How can we see how Read more

Leave a Reply

Your email address will not be published. Required fields are marked *

To prove you're a person (not a spam script), type the security word shown in the picture.
Anti-spam image