File list (file upload) from the front end using WP REST API
-
Dears,
I already managed to update/create a custom post type with custom fields from the front end using the WP REST API. Now I am trying to add a multi image uploader to the form using the field type: file_list but with no success.
The important thing in WP API is to expose the custom fields to the API. I managed to do that for a simple custom field like text: Say the key is “_item_price” and the post type is “forsale”
add_action( 'rest_api_init', function() { register_api_field( 'forsale', '_item_price', array( 'get_callback' => 'slug_get_itemprice', 'update_callback' => 'slug_update_itemprice', 'schema' => null ) ); }); function slug_get_itemprice( $object, $field_name, $request ) { return get_post_meta( $object['id'], $field_name ); } // function slug_get_itemprice function slug_update_itemprice( $value, $object, $field_name ) { return update_post_meta( $object->ID, $field_name, strip_tags( $value) ); } // function slug_update_itemprice
That works fine, I can save a new item price of a product from the front end AJAX with something easy like:
formData.append('_item_price', price);
Now I would like to do the same with a file_list field say it is key is “_item_images”, but I think I am stuck with the update_post_meta since I am not sure how you save this type of data.
Your help is appreciated.
- The topic ‘File list (file upload) from the front end using WP REST API’ is closed to new replies.