SEO

How to Automatically Fill Title, Caption, Alt Text, and Description on WordPress Images

by Dominika K.

WordPress is one of the most popular website building platforms in the world today. It’s used by millions of website owners for blogging purposes, just because it’s easy to use and doesn’t require a lot of specialized knowledge or training. Any person with a basic understanding of the Web can register their website and get started on this platform. The good thing about WordPress is that it is entirely free to use, though certain features and plugins will have to be bought.

How to Rank in Google Image Search

The platform also contains a litany of features that make it easy for you to upload new posts, schedule them for publishing at specific time periods, and make as many changes as you want directly through the graphical user interface. WordPress also allows users to upload images and videos to their blog posts to make them more engaging and exciting, offering maximum website customization. Furthermore, You can use a simple drag-and-drop option to do all of this.

How to Automatically Fill Title, Caption, Alt Text, and Description on WordPress Images

The Problem

However, for people who are uploading many images inside their blog posts, it can be pretty time consuming to fill out the Alt Text, the Title, the Caption, and the Description all by yourself. It’s essential that you fill in the text since Google focuses on such minor things and it could affect the SEO ranking of your website.

Moreover, viewers also look at the titles and the captions with each image to determine what you are trying to show. By default, WordPress will create a title for your picture as soon as you upload it. Image the title is created merely base when it uploads.How to Automatically Fill Title, Caption, Alt Text, and Description on WordPress Images

The problem arises when you take a look at the Title that WordPress automatically creates. WordPress isn’t very sophisticated when creating the titles, and only removes the extraneous spaces you put in the file name and separates them. That’s about it. So, if you uploaded an image that had a title of “image 454,” WordPress would merely rename it "Image 454". Not very helpful, is it?

Original Titles

If there are underscores or dashes in the title, WordPress will let them be as is. It doesn’t even correctly capitalize the words as you would expect. Therefore, if you do not tinker with the settings and leave them at default, the image title will be virtually the same as the file name used for the image. The only thing that will change is WordPress removing the extra spaces from the file name. This isn’t the solution most people are looking for regarding website customization.

Decoration

Hi, Let Us Help!

We are a team of web experts, that delivers results. Web development and design is our passion.

A more significant issue you have to worry about is that WordPress will not automatically set the Alt-Text for the image either. For SEO, your images are required to have a correctly specified Alt-Text. It costs virtually nothing to put up and could make a significant difference in improving visibility to your website. There’s no argument here; you have to put up Alt-Text for your images if you want Google and other major search engines to take notice.

Now, you can either enter the Alt-Text manually for every image you upload by going to the Edit Media page, but that’s quite a hassle in itself. Thankfully, there is a more streamlined approach you can follow, which won’t just help you fill out the Alt-Text, but also allow you to set the caption of the image as well as the description using an explicit file name.

See more:  Must Have SEO Tricks To Increase Traffic On Your WordPress Website

How to Automatically Fill Title, Caption, Alt Text, and Description on WordPress Images

The Solution

Every time you upload a new media file to WordPress, it creates a new Attachment. Simply put, an Attachment is another post type. Thus, has a title, excerpt, and all the other things an original post would be expected to have. On top of that, the attachment is also likely to have metadata with it as well. The Alt-Text, in this case, would be the metadata that is associated with the attachment.
Therefore you will divide your approach into following steps:

- Upload a new image that creates an Attachment.
- Update the Title, Excerpt, and the Content to the Attachment.

Finally, you can alter the metadata to set the Alt-Text. To automatically update the Title and Caption, you will need to use two WordPress functions and a hook.
The `add_attachment` hook will help us, and the first function we will use is the `wp_update_post ()` function in order to update the `wp_posts` table.
The next function we will use is the `update_post_meta ()` function to set up the Alt-Text.

Instructions

First of all, rename your files to a concise, easily understandable phrase.
Simply copy the PHP code listed below into the theme’s `functions.php` file.
Finally, upload the images into the WordPress library.

/* Automatically set the image Title, Alt-Text, Caption & Description upon upload
-----------------------------------------------------------------------*/

add_action( 'add_attachment', 'my_set_image_meta_upon_image_upload' );

function my_set_image_meta_upon_image_upload( $post_ID ) {
// Check if uploaded file is an image, else do nothing
if ( wp_attachment_is_image( $post_ID ) ) {
$my_image_title = get_post( $post_ID )->post_title;
// Sanitize the title: remove hyphens, underscores & extra
// spaces:
$my_image_title = preg_replace( '%\s*[-_\s]+\s*%', ' ',
$my_image_title );
// Sanitize the title: capitalize first letter of every word
// (other letters lower case):
$my_image_title = ucwords( strtolower( $my_image_title ) );
// Create an array with the image meta (Title, Caption,
// Description) to be updated
// Note: comment out the Excerpt/Caption or Content/Description
// lines if not needed
$my_image_meta = array(
// Specify the image (ID) to be updated
'ID' => $post_ID,
// Set image Title to sanitized title
'post_title' => $my_image_title,
// Set image Caption (Excerpt) to sanitized title
'post_excerpt' => $my_image_title,
// Set image Description (Content) to sanitized title
'post_content' => $my_image_title,
);

// Set the image Alt-Text
update_post_meta( $post_ID, '_wp_attachment_image_alt',
$my_image_title );
// Set the image meta (e.g. Title, Excerpt, Content)
wp_update_post( $my_image_meta );
}
}

Using this code in the Title, Alt-Text, etc. it will fill them based on the na me of the file. No need for manual modification of meta data for each image on the site. This is a pretty handy little piece of code that will make your life a lot easier.

Decoration

Need Help With your Images?

Our Experts can handle any customization or development at a reasonable price.

Image optimization in Wordpress is very easy. I hope the article will be helpful to you. you have any questions or comments please let us know and we will try our best to answer them the best we can.

Dominika K.

Disqus Comments

Reading time 6 minutes

Subscribe to our newsletter

I agree This website uses cookies to ensure you get the best experience. More info