最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

plugins - How to modify WCMP Rest API response?

programmeradmin2浏览0评论

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?

Share Improve this question asked Apr 29, 2020 at 13:07 Ahmed El-AtabAhmed El-Atab 1315 bronze badges 3
  • 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
Add a comment  | 

1 Answer 1

Reset to default 0

Finally 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...

发布评论

评论列表(0)

  1. 暂无评论