I had tried the register_rest_field()
to modify the Rest API response of the WCMP plugin, but it seems that this function applies only to the default wordpress REST API (wp/v2).
The purpose of modifying is that vendors data (fetched from this URL ...wp-json/wcmp/v1/vendors) does not include the image's URL for each vendor, instead the image ID is returned, and I don't want to make another API request just to get the image URL based on the ID.
Adding the _embed
parameter did not work.
How can I modify the response of the WCMP like the register_rest_field()
do with default REST API in order to include the image URL?
I had tried the register_rest_field()
to modify the Rest API response of the WCMP plugin, but it seems that this function applies only to the default wordpress REST API (wp/v2).
The purpose of modifying is that vendors data (fetched from this URL ...wp-json/wcmp/v1/vendors) does not include the image's URL for each vendor, instead the image ID is returned, and I don't want to make another API request just to get the image URL based on the ID.
Adding the _embed
parameter did not work.
How can I modify the response of the WCMP like the register_rest_field()
do with default REST API in order to include the image URL?
- The plugin would need to support its own filters or functions for modifying the API response, and they will be specific to the plugin. 3rd-party plugins are off topic here, so I recommend contacting the plugin author. – Jacob Peattie Commented Apr 29, 2020 at 13:20
- Thanks for your comment. Is there a way to get the image URL from ID without fetching the REST API Media? – Ahmed El-Atab Commented Apr 29, 2020 at 13:38
- You would need to make an API request of some kind. – Jacob Peattie Commented Apr 29, 2020 at 13:46
1 Answer
Reset to default 0Finally I had solved it!
In the source code of the plugin "dc-woocommerce-multi-vendor", I had viewed the class "class-wcmp-rest-vendors-controller.php" and figured out that they are using this filter in order to gather up the fields of the response:
apply_filters("wcmp_rest_prepare_vendor_object_args", array(...));
In the functions.php of the child theme I had written this code to edit the filter:
function modify_rest_api_vendor_response( $arg ) {
$arg["shop"]["image"]=wp_get_attachment_url($arg["shop"]["image"]);
return $arg;
}
add_filter( 'wcmp_rest_prepare_vendor_object_args', 'modify_rest_api_vendor_response', 10, 3 );
And now I can get the image URL
Hope that it could help somone...