Skip to content

Inserter media: save item metadata on the uploaded attachment#80370

Open
thrijith wants to merge 5 commits into
WordPress:trunkfrom
thrijith:add/60378-inserter-media-attachment-meta
Open

Inserter media: save item metadata on the uploaded attachment#80370
thrijith wants to merge 5 commits into
WordPress:trunkfrom
thrijith:add/60378-inserter-media-attachment-meta

Conversation

@thrijith

Copy link
Copy Markdown
Contributor

What?

When an image from an inserter media category (Openverse or one registered via registerInserterMediaCategory()) is uploaded to the media library, the item's title, alt, and new optional description are now saved on the attachment. A new optional filename property sets the uploaded file's name. data:/blob: URLs no longer have a filename derived from them.

Fixes #60378.

Why?

Only caption was forwarded to the upload. Title and alt text reached the block but not the attachment, so the media library filled with items that had no alt text and a URL-derived title and filename (e.g. image-n.png for generated images). Image-generation providers like ClassifAI (see 10up/classifai#729) know the prompt at insert time and need a way to store it as the attachment's title, alt text, and description.

How?

  • MediaPreview's onMediaInsert adds alt_text, title, and description to the upload's additionalData, each only when the item provides a value, so server-side defaults still apply otherwise.
  • New optional filename and description properties on InserterMediaItem.
  • The uploaded file is named media.filename || getFilename( url ) || 'image.jpg', skipping getFilename() for data:/blob: URLs since they contain no filename. Previously the derived name for a data: URL had no valid extension and the upload could fail.

Known limitation: drag-and-drop from the media tab uses the image block's sideload path and doesn't carry item metadata; that needs cross-package plumbing and is left as a follow-up.

Testing Instructions

  1. Register a test category in the browser console on a post editor screen:

    wp.data.dispatch( 'core/block-editor' ).registerInserterMediaCategory( {
        name: 'test-media', labels: { name: 'Test', search_items: 'Search' },
        mediaType: 'image', isExternalResource: true,
        async fetch() { return [ {
            sourceId: 1, title: 'My Title', alt: 'My alt text',
            description: 'My description', filename: 'my-image.jpg',
            url: '<same-origin image URL>', previewUrl: '<same-origin image URL>',
        } ]; },
    } );
  2. Inserter → Media → Test → click the item; wait for "Image uploaded and inserted."

  3. Media Library → newest attachment: file is my-image.jpg; title, alt text, and description match the item.

  4. Repeat with a data:image/png;base64,… url and no filename: upload succeeds, file named image.png.

  5. Openverse: insert a CORS-allowed result (e.g. "Vincent van Gogh sunflowers painting") — attachment title matches the item title.

  6. npm run test:unit -- packages/block-editor/src/components/inserter/media-tab/test/media-preview.js — 6 tests pass.

@github-actions

github-actions Bot commented Jul 16, 2026

Copy link
Copy Markdown

Warning: Type of PR label mismatch

To merge this PR, it requires exactly 1 label indicating the type of PR. Other labels are optional and not being checked here.

  • Required label: Any label starting with [Type].
  • Labels found: [Package] Block editor.

Read more about Type labels in Gutenberg. Don't worry if you don't have the required permissions to add labels; the PR reviewer should be able to help with the task.

@github-actions

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: thrijith <thrijith@git.wordpress.org>
Co-authored-by: Sidsector9 <nomnom99@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@github-actions github-actions Bot added the [Package] Block editor /packages/block-editor label Jul 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Package] Block editor /packages/block-editor

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add ability to set meta data of the attachment via registerGenerateImageMediaCategory()

1 participant