0

I have a website with 400+ images. Using a spreadsheet plugin I've been through and manually added alt text to each of the images.

The problem I have is that the pages are built with a page builder and a lot of the images where added in text blocks using <img src="https://example.com/image-folder/image-name.jpg"> and therefore haven't dynamically pulled in the alt text.

I'm trying to avoid having to go through all of the pages, find the img code, work out which image it is, look up the alt text and manually type it, again!

I'm thinking there must be a way to reference the image in the database by the img src, then get the alt text from the database that way.

I'd like to write a function that sits in the functions file and I can call from the alt text. Something like this get_alt_text():

<img src="https://example.com/image-folder/image-name.jpg" alt="<?php echo( get_alt_text() ); ?>">

This way I can just paste in a single bit of text into each image alt which would save a ton of time.

It seems there are functions that could help me, but it's a bit more than I fully understand right now and wondered if someone could at least say (with confidence) that it's possible or not.

For example this Wordpress "attachment_url_to_postid" function.

or this "wp_get_attachment_image_src" function

My thinking for the function is:

  • Get the img src
  • match that to an image in the database
  • get that image ID and meta data etc
  • separate the alt text and pass that back

So, my question is:

  • Can you tell me if this is possible?
  • Even better, point me in the right direction.
  • Even better yet! Show an example.

Thank you for your time.

(I posted this on Stackoverflow but haven't heard from anyone, so I'm trying here)

1 Answer 1

0

Did you get anywhere with this? I've just written a plugin that does exactly this job. It's basically a cron job that scans all your posts & pages to create an internal index of which images are where. It then synchronises your Media Library alt text with the images in your content. It's based on the code from this tutorial: https://wp-tutorials.tech/refine-wordpress/filter-for-images-missing-alt-text/ but the plugin builds on it to add the synchronisation stuff. There's al link to the plugin from the tutorial page.

But... it all comes down to page-builder integration. The plugin currently only has Gutenberg and Classic-Edit integration at the moment. I need to add Elementor/Divi/etc integrations... a bit of a pain, but it has to be done.

4
  • Amazing! Thanks for replying and nice one on creating the plugin. I didn't get anywhere with it. I had to move on to other stuff but the problem still exists. The website in question uses Page Builder by SiteOrigin. I think that will be a super useful plugin. I ended up using WP Sheet Editor to easily go through and edit the alt text, but of course because the page builder had pulled in the data from an earlier date, the images don't have the new alt text. Keep up the great work and I hope one day it works with Page Builder by SiteOrigin. I'll keep it in mind for other projects though. Thanks
    – Steviehype
    Aug 1, 2022 at 13:16
  • Ah right. It's interesting to know that it's not just us who have bumped into this issue of synchronising and bulk-updating Alt Texts. We'll get some more work done on the plugin to grow it some more.
    – Paul
    Aug 2, 2022 at 14:09
  • I think its one of those things that's a really daunting, time consuming thing to do. We all know we should add alt text as we go but sometimes it's doesn't happen. Making it easy to do at a later date is something I'm sure others will want to do!
    – Steviehype
    Aug 3, 2022 at 15:10
  • It's been really useful to use the Media Library as the single source of truth for Alt Texts. The PITA form our end is having to write integrations for all the page builders. It's worth it though - so much easier being able to edit in the Media Library and have the code push the new Alt Texts to all the content. I wonder if there's anything else we can do, now that the plugin holds an index of which images are in which posts/pages?
    – Paul
    Aug 4, 2022 at 16:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.