How to: Automatically Set Alt Text for Images in WordPress
Setting alt (alternative) text on your website’s images is one of the most important – yet sometimes forgotten – best practice for website accessibility. Alt text is the short description of an image that helps screen readers and search engines better understand the contents of that image.
Search engines use the alt text of an image to display them in relevant search results, and if an image fails to load in a browser, the alternative text is displayed instead.
We’ve talked before about best practices for responsive and accessible images, but how can you more easily add alt text to your images in WordPress?
Here’s how to automatically add alt text to images uploaded in WordPress.
How to Set Alt Text Automatically on Upload
When you upload an image to WordPress, the filename is automatically set as the “title” of the image.
But besides displaying as a tooltip when hovering over the image, the “title” attribute doesn’t serve any other purpose and is usually ignored by screen readers and search engines.
We can actually use this built-in function to automatically set the alt text of an image when it’s uploaded.
Key to this approach: the file name you set for the image should be descriptive enough that it makes sense as alt text. We don’t want the alt text to say FILE_91365.JPG!
To automatically set the image’s filename as the alt text, add the following code to the functions.php file in your WordPress theme’s folder.
add_action( 'add_attachment', 'dgtlnk_set_image_meta_upon_image_upload' );
function dgtlnk_set_image_meta_upon_image_upload( $post_ID ) {
if ( wp_attachment_is_image( $post_ID ) ) {
$my_image_title = get_post( $post_ID )->post_title;
$my_image_title = preg_replace( '%\s*[-_\s]+\s*%', ' ', $my_image_title );
$my_image_title = ucwords( strtolower( $my_image_title ) );
$my_image_meta = array(
'ID' => $post_ID,
'post_title' => $my_image_title,
);
update_post_meta( $post_ID, '_wp_attachment_image_alt', $my_image_title );
wp_update_post( $my_image_meta );
}
}
Let’s review the code.
First, we hook into WordPress’s add_attachment hook that fires when an image is uploaded.
Next, in our callback function, we first confirm that the uploaded file is an image using the wp_attachment_is_image() function. If it is an image, we save the image’s title (assigned by WordPress in the $my_image_title variable) and use preg_replace to replace any hyphens, underscores and spaces with a single space. Then, we capitalize the first letter of every word. After that, we assign the updated text as the alt text for the image using update_post_meta() function and update the image post to save it all.
Now, when an image is uploaded via the Media Library in the admin, the image’s alt text will be automatically set based on the image’s descriptive filename. This also makes searching for images in the Media Library much easier as the file names are descriptions of images.
Using AI and Plugins to Set Alt Text for Your Images
If you’d like to get longer, more descriptive alt text for your images, don’t feel bad about using AI tools. Uploading your images to your AI tool of choice and asking it to generate descriptive alt text is a great way to speed up the process, and won’t hurt your authenticity.
You’ll still need to copy and paste the alt text after you upload the image, but it saves you the step of writing the descriptive text for each image.
There are even some WordPress plugins that combine both of these methods – using AI to generate the alt text while automatically applying it to the images when they’re uploaded. These plugins include:
Each plugin is a little different, and pricing varies, so do your research before installing and signing up for a service. Generally, they each require either a monthly subscription or purchase of tokens to use, while others can hook into an existing AI subscription you already have.
If you also need help setting the image’s alt text automatically on upload or for any other digital marketing needs, reach out to us and we’d be more than happy to help.