![](/img/trans.png)
[英]Update WooCommerce Product with Custom Wordpress API Endpoint
[英]WordPress custom API with WooCommerce Authentication
我已經在 WordPress 中創建了自定義 API 並且我得到了 WooCommerce 訂閱數據在這個 ZDB974238714CA38DE634A7CE1D08 我的要求。
但是,現在我想在此 API 中添加基本身份驗證,它可以像其他 WooCommerce API 端點一樣檢查使用者密鑰和秘密。
這是我的示例 API 看起來我想檢查基本身份驗證。
// Action to execute Rest API routes
add_action('rest_api_init', function () {
// Getting Product data based on subscription id
register_rest_route('getproductdata', '/(?P<id>\d+)', array(
'methods' => 'GET',
'callback' => 'getProductData',
));
});
function getProductData($request) {
// I WANT TO CHECK BASIC AUTHENTICATION HERE BEFORE EXECUTING BELOW CODE
die('inside my api');
}
I have checked this https://woocommerce.github.io/woocommerce-rest-api-docs/#authentication-over-http and https://wordpress.stackexchange.com/questions/355041/how-to-authenticate-custom -api-endpoint-in-woocommerce這個網址,但我還沒有找到合適的方法或過濾器或教程來滿足我的要求。
至少有人可以指導我如何在此處添加身份驗證..任何建議都將受到高度贊賞。
謝謝
在這種情況下,您可以使用permission_callback
鍵:
// Action to execute Rest API routes
add_action('rest_api_init', function () {
// Getting Product data based on subscription id
register_rest_route('getproductdata', '/(?P<id>\d+)', array(
'methods' => 'GET',
'callback' => 'getProductData',
'permission_callback' => 'restCheckUser'
));
});
function getProductData($request) {
// I WANT TO CHECK BASIC AUTHENTICATION HERE BEFORE EXECUTING BELOW CODE
die('inside my api');
}
function restCheckUser(WP_REST_Request $request) {
if ('get_token_from_database' === $request->get_param('token')) {
return true;
}
return false;
}
完整文檔在這里 - https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-custom-endpoints/
好的,伙計們.. 最終,我使用JWT WP REST API 的身份驗證在我的自定義端點 API 中進行了身份驗證。
感謝大家的幫助、支持和指導。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.