Make WordPress Core

Opened 6 years ago

Closed 5 years ago

#44567 closed defect (bug) (fixed)

Image creation via Rest API, including additional data/meta data

Reported by: apermo's profile apermo Owned by: timothyblynjacobs's profile TimothyBlynJacobs
Milestone: 5.4 Priority: normal
Severity: normal Version: 4.7
Component: REST API Keywords: has-patch has-unit-tests
Focuses: Cc:

Description

When creating a new attachment with the Rest API, that also includes data, normal data like the title will be handled, but additional data, like meta data for example are not handled.

When adding this code near the end of the method create_item() on the file class-wp-rest-attachments-controller.php the issue is solved.

$request->set_url_params( [ 'id' => $id ] );             
                                                               
$response = parent::update_item( $request );             
                                                               
if ( is_wp_error( $response ) ) {                        
        return $response;                                    
}                                                        

I added it prior $fields_update...

Note: Since this requires an upload with data and registered meta, I've added an example file. And you need to register the meta on the website too.

register_meta( 'post', 'my_meta', [
			'type' => 'string',
			'description' => '',
			'single' => true,
			'show_in_rest' => true,
		] );

Attachments (4)

upload_with_attachment.php (1.4 KB) - added by apermo 6 years ago.
Dummy file for uploading an attachment via rest api including title and a meta value
44567.diff (3.0 KB) - added by TimothyBlynJacobs 5 years ago.
44567.2.diff (2.7 KB) - added by TimothyBlynJacobs 5 years ago.
44567.3.diff (2.7 KB) - added by kadamwhite 5 years ago.
Fix test sample text strings.

Download all attachments as: .zip

Change History (11)

@apermo
6 years ago

Dummy file for uploading an attachment via rest api including title and a meta value

This ticket was mentioned in Slack in #core-restapi by apermo. View the logs.


6 years ago

This ticket was mentioned in Slack in #core-restapi by apermo. View the logs.


6 years ago

#3 @TimothyBlynJacobs
5 years ago

  • Keywords has-patch has-unit-tests added
  • Milestone changed from Awaiting Review to 5.4
  • Owner set to TimothyBlynJacobs
  • Status changed from new to assigned
  • Version changed from 4.9.7 to 4.7

Thanks for the report @apermo!

I've uploaded a patch to add support for setting meta while creating an attachment. Could you let me know if it fixes your issue?

This ticket was mentioned in Slack in #core-restapi by timothybjacobs. View the logs.


5 years ago

#5 @TimothyBlynJacobs
5 years ago

Uploaded an additional patch removing the change to an unrelated test.

This ticket was mentioned in Slack in #core-restapi by timothybjacobs. View the logs.


5 years ago

@kadamwhite
5 years ago

Fix test sample text strings.

#7 @kadamwhite
5 years ago

  • Resolution set to fixed
  • Status changed from assigned to closed

In 47261:

REST API: Allow meta to be set when creating a new media record via REST.

Props TimothyBlynJacobs, apermo.
Fixes #44567.

Note: See TracTickets for help on using tickets.