If you want to enhance your WordPress site’s SEO and accessibility by adding alt, title, and description attributes to your images automatically, you can do so without using any plugins. By adding a few lines of custom code to your theme’s functions.php
file, you can ensure that every image you upload has these attributes correctly formatted. Here’s a step-by-step guide on how to do this.
Step 1: Access Your Theme’s functions.php
File
- Log in to your WordPress dashboard.
- Go to Appearance > Theme Editor.
- From the list of theme files on the right, select
functions.php
.
Step 2: Add the Custom Function
Copy and paste the following code into your functions.php
file:
function update_image_attributes($post_ID) {
// Get the attachment
$attachment = get_post($post_ID);
// Only proceed if this is an attachment
if ($attachment && 'attachment' == $attachment->post_type) {
// Get the file name
$file_name = $attachment->post_name;
// Remove numbers and replace dashes with spaces
$file_name = preg_replace('/-\d+/', '', $file_name);
$file_name = str_replace('-', ' ', $file_name);
// Capitalize the first letter of each word
$title = ucwords($file_name);
// Update title and description
$updated_attachment = array(
'ID' => $post_ID,
'post_title' => $title,
'post_content' => $title,
);
// Update the post
wp_update_post($updated_attachment);
// Update alt text
update_post_meta($post_ID, '_wp_attachment_image_alt', $title);
}
}
// Hook into the 'add_attachment' action
add_action('add_attachment', 'update_image_attributes');
Step 3: Save Your Changes
After pasting the code, click the Update File button to save your changes.
How the Code Works
- Function Definition: The
update_image_attributes
function is defined to handle the updating of image attributes. - Get Attachment: It retrieves the attachment post using
get_post($post_ID)
. - Check Attachment Type: It checks if the post is an attachment.
- Process File Name: It removes numbers from the file name, replaces dashes with spaces, and capitalizes the first letter of each word to create a properly formatted title.
- Update Attributes: It updates the
title
anddescription
fields of the attachment. - Set Alt Text: It sets the alt text using
update_post_meta
. - Hook into Upload Action: The function is hooked into the
add_attachment
action so that it runs automatically whenever a new image is uploaded.
Benefits of This Approach
- SEO Optimization: Proper alt text and titles improve your site’s SEO.
- Accessibility: Alt text helps screen readers describe images to visually impaired users.
- Automation: Automatically updating these attributes saves time and ensures consistency.
By following this guide, you can enhance your WordPress site’s image attributes without relying on additional plugins, keeping your site lightweight and efficient.
If you want to add the caption at the same time, copy the following code:
function update_image_attributes($post_ID) {
// Get the attachment
$attachment = get_post($post_ID);
// Only proceed if this is an attachment
if ($attachment && 'attachment' == $attachment->post_type) {
// Get the file name
$file_name = $attachment->post_name;
// Remove numbers and replace dashes with spaces
$file_name = preg_replace('/-\d+/', '', $file_name);
$file_name = str_replace('-', ' ', $file_name);
// Capitalize the first letter of each word
$title = ucwords($file_name);
// Update title, alt text, and description
$updated_attachment = array(
'ID' => $post_ID,
'post_title' => $title,
'post_content' => $title,
'post_excerpt' => $title,
);
// Update the post
wp_update_post($updated_attachment);
// Update alt text
update_post_meta($post_ID, '_wp_attachment_image_alt', $title);
}
}
// Hook into the 'add_attachment' action
add_action('add_attachment', 'update_image_attributes');