[wp-trac] [WordPress Trac] #64215: slimImageObject drops meta fields added to attachments
WordPress Trac
noreply at wordpress.org
Fri Nov 7 13:22:08 UTC 2025
#64215: slimImageObject drops meta fields added to attachments
--------------------------+-----------------------------
Reporter: leemon | Owner: (none)
Type: defect (bug) | Status: new
Priority: normal | Milestone: Awaiting Review
Component: Media | Version:
Severity: normal | Keywords: needs-patch
Focuses: |
--------------------------+-----------------------------
I've opened an issue in the Gutenberg repo, but since the solution likely
involves modifying core code, I'm creating a ticket here as well.
### Description
The object passed to the `onSelect` callback in the Gutenberg
`MediaUpload` component calls the following function that removes all
image attributes except those in the `attrSet` constant:
{{{
const slimImageObject = ( img ) => {
const attrSet = [
'sizes',
'mime',
'type',
'subtype',
'id',
'url',
'alt',
'link',
'caption',
];
return attrSet.reduce( ( result, key ) => {
if ( img?.hasOwnProperty( key ) ) {
result[ key ] = img[ key ];
}
return result;
}, {} );
};
}}}
That means that meta fields added to attachments via `register_post_meta`
and the `attachment_fields_to_edit` and `attachment_fields_to_save`
filters are dropped. Is there a way to avoid this?
### Step-by-step reproduction instructions
1. Register a meta field to the attachment post type:
{{{
register_post_meta(
'attachment',
'_oembed_url',
array(
'show_in_rest' => true,
'type' => 'string',
'single' => true,
'sanitize_callback' => 'sanitize_url',
'auth_callback' => function () {
return current_user_can( 'edit_posts' );
},
)
);
}}}
2. Add a `MediaUpload` component to a block:
{{{
<MediaUpload
allowedTypes={['image']}
multiple={true}
gallery={true}
value={images.map((image) => image.id)}
onSelect={setImages}
render={({ open }) => (
<ToolbarButton onClick={open}>
{__('Edit images')}
</ToolbarButton>)}
/>
}}}
3. Check that the `value` param doesn't contain any meta fields:
{{{
const setImages = (value) => {
console.log(value);
}
}}}
--
Ticket URL: <https://core.trac.wordpress.org/ticket/64215>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list