admin管理员组文章数量:1387327
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...
本文标签: pluginsHow to modify WCMP Rest API response
版权声明:本文标题:plugins - How to modify WCMP Rest API response? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744519385a2610363.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论