Optimising WordPress Websites: Using Media Library ALT Tags for SEO

September 14, 2023

All websites, including WordPress websites, must optimize for SEO. Search Engine Optimization (SEO) is vital for any website, ensuring it ranks well on search engine results pages. One often-overlooked aspect of SEO is the use of Alt tags for images. Alt tags provide valuable information to search engines and improve accessibility for users with disabilities. If you’re using WordPress, you can easily harness the power of your Media Library Alt tags on the frontend by following this guide.

code Snippet for your WordPress website:

// Load WordPress environment

// Function to add or update ALT text for an image
function add_update_image_alt_text($attachment_id, $alt_text) {
    update_post_meta($attachment_id, '_wp_attachment_image_alt', $alt_text);
    echo "ALT text updated for Attachment ID $attachment_id\n";

// URL to the CSV file
$csvUrl = 'https://example.com/document.csv'; // Replace with the actual URL

// Get CSV content from URL
$csvContent = file_get_contents($csvUrl);

// Convert CSV content to an array
$lines = explode("\n", $csvContent);

foreach ($lines as $line) {
    $data = str_getcsv($line);
    if (count($data) >= 2) {
        $image_url = trim($data[0]);
        $alt_text = trim($data[1]);
        // Find the attachment ID based on the image URL
        $attachment_id = attachment_url_to_postid($image_url);
        // If an attachment is found, update ALT text
        if ($attachment_id) {
            add_update_image_alt_text($attachment_id, $alt_text);
        } else {
            echo "Image not found for URL: $image_url\n";

echo "ALT text update process completed.\n";

This code allows you to update Alt text for images in your WordPress Media Library based on data from a CSV file.

step by step guide on how to use This on your WordPress Website:

  1. Load WordPress Environment: Start by including ‘wp-load.php’ to access WordPress functions.
  2. add_update_image_alt_text Function: This function updates Alt text for an image using the update_post_meta function. It’s crucial for automating Alt tag updates.
  3. CSV File URL: Specify the URL of your CSV file. In this example, we use ‘https://example.com/document.csv’. Replace it with your actual CSV file URL.
  4. CSV Processing Loop: The code reads the CSV file line by line, splitting each line into an array.
  5. Extract Image URL and Alt Text: For each line, it extracts the image URL and Alt text. Ensure that your CSV follows the format: “Image URL, Alt Text.”
  6. Find Attachment ID: It uses attachment_url_to_postid to find the attachment ID based on the image URL.
  7. Update Alt Text: If an attachment is found, it updates the Alt text using the add_update_image_alt_text function.
  8. Image Not Found: If an image is not located, it provides a message indicating the absence of the image
  9. Completion Message: Upon completing the ALT text update process, it finally displays a completion message

Using this code, you can efficiently manage Alt text for images in your WordPress Media Library, enhancing your website’s SEO and user experience. Remember to regularly update your Alt tags with descriptive and relevant text to improve your website’s accessibility and search engine ranking.

If you are interested in additional services and guidelines on how to optimize your website, please Get in touch with us.



Digital Agency Cape Town

© 2023 | Trinity Managed Solutions (PTY) LTD
Privacy Policy