Skip to content
Neartool
Checking…

WordPress Media Library Bulk Alt Text: JSON Mapping

Neartool's WordPress export is a JSON file keyed by file name, with alt, caption and description for each image. WordPress core has no importer for it, so you apply it yourself: set each attachment's alt text with WP-CLI or a script, after testing on a copy.

Sample file

Three sample entries, generated by the WordPress JSON export code.

{
  "team-photo-2024.jpg": {
    "alt": "Six colleagues standing in front of a glass office entrance",
    "caption": "Six colleagues stand together in front of a glass office entrance",
    "description": "Six colleagues stand together in front of a glass office entrance. They are smiling and dressed casually."
  },
  "sales-chart-q3.png": {
    "alt": "Bar chart showing sales rising each month from July to September",
    "caption": "A bar chart of monthly sales for the third quarter",
    "description": "A bar chart of monthly sales for the third quarter. The bars grow taller from July to September."
  },
  "workshop-bench.jpg": {
    "alt": "Wooden workbench with hand tools hung on a pegboard",
    "caption": "A wooden workbench in a workshop with hand tools hung neatly on a pegboard above it",
    "description": "A wooden workbench in a workshop with hand tools hung neatly on a pegboard above it."
  }
}

Steps

  1. Generate alt text for the images in your media library and review it.
  2. Choose the WordPress JSON export. Each entry is keyed by the image's file name.
  3. Find the attachment ID for each file name in your site. The media library list or a WP-CLI query shows it.
  4. Set the alt text with WP-CLI, for example wp post meta update followed by the ID, _wp_attachment_image_alt and the text.
  5. Run the script on a staging copy first, then spot-check a few images on the live site.

Start with the Alt text generator, then check the text with the alt text checker.

How the file is laid out

The file is a single JSON object. Each key is an image file name, and each value has three fields: alt, caption and description. Alt is the text for the alt attribute. Description is the longer sentence the model wrote. Caption is the first sentence of that description, or the alt text when there is no description.

The structure is a convenience for scripts. WordPress itself does not read this file, and nothing in core imports it.

Where WordPress keeps alt text

Every item in the media library is a post of type attachment. Its alt text is saved as post meta under the key _wp_attachment_image_alt. The caption lives in the attachment's excerpt, and the description in its content. WP-CLI's post meta update command can write the alt text when you give it the attachment ID, the key and the value.

A file name is not the same as an ID, and the same file name can exist more than once, for example after re-uploads. Resolve each name to an ID first, and decide what to do when you find none or more than one.

A safe way to run a bulk update

Work on a staging copy, or take a full database backup. Write the script so it only changes images whose alt text is currently empty, which avoids overwriting text a person has already improved. Log every change, and spot-check a sample against the site.

If you do not use the command line, a plugin that edits alt text in bulk can do the same job. Check it is maintained and test it on a small set.

A note on captions and descriptions

Visible captions appear under the image on the page, so only use a caption when you want readers to see it. Writing the same sentence into alt text and a caption makes screen reader users hear it twice. Treat the caption and description fields in the file as optional drafts.

Limits and facts

Limits and facts for WordPress Media Library Bulk Alt Text: JSON Mapping
Alt text storage_wp_attachment_image_alt on the attachment
Core importer for this fileNone
File keysImage file names, which are not unique IDs
Fields per imagealt, caption, description

Frequently asked questions

Can WordPress import this JSON file?

No. WordPress core has no importer for it. The file is meant for a script or for you to read from while you edit the media library.

How do I set alt text with WP-CLI?

Use the post meta update command with the attachment's ID, the key _wp_attachment_image_alt and the new text. WP-CLI documents the command syntax on its developer site.

How do I find an image's attachment ID?

In the media library, open the image and look at the number in the address bar. In scripts, query attachments and match on the file name, and handle duplicates.

Should I fill the caption field as well?

Only if you want the caption shown under the image. Captions are visible, so they should add something that alt text does not.

Will this overwrite my existing alt text?

Only if your script does. A good script skips images that already have alt text, or logs the old value before it changes anything.