概要
woocommerce_rest_api_get_rest_namespaces フィルタは、WooCommerceのREST APIにおける名前空間を変更・追加するために使用されます。このフィルタを利用することにより、開発者は自分のカスタムエンドポイントを定義することができます。これにより、特定の機能やサービスを提供する際の柔軟性が高まり、様々な用途に対応可能です。以下は、このフィルタが特によく使われる場面です。
- カスタムREST APIエンドポイントの作成
- 既存のAPIエンドポイントの拡張
- 特定条件でのエンドポイントの公開・非公開設定
- リクエスト処理のカスタマイズ
- 新たなデータソースの統合
- 外部システムとの連携
構文
add_filter('woocommerce_rest_api_get_rest_namespaces', 'your_function_name');
function your_function_name($namespaces) {
// ここで処理を追加
return $namespaces;
}
パラメータ
$namespaces: 既存のREST API名前空間の配列。
戻り値
- 変更後の名前空間の配列。
使用可能バージョン
- WooCommerce: 3.0.0以降
- WordPress: 4.4以降
この関数のアクションでの使用可能性
| アクション名 | 使用例 |
|---|---|
| mu_plugin_loaded | |
| registered_post_type | |
| plugins_loaded | |
| wp_roles_init | |
| setup_theme | |
| after_setup_theme | |
| set_current_user | |
| init | |
| register_sidebar | |
| wp_loaded | |
| send_headers | |
| parse_query | |
| pre_get_posts | |
| wp | |
| template_redirect | |
| get_header | |
| wp_head |
サンプルコード
サンプルコード1
add_filter('woocommerce_rest_api_get_rest_namespaces', 'add_custom_namespace');
function add_custom_namespace($namespaces) {
$namespaces['custom_namespace/v1'] = array(
'routes' => array(),
);
return $namespaces;
}
このサンプルでは、custom_namespace/v1という新しい名前空間を追加しています。この名前空間に関連するルートは現在は空ですが、後に追加することができます。
引用元: https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-custom-endpoints/
サンプルコード2
add_filter('woocommerce_rest_api_get_rest_namespaces', 'modify_existing_namespace');
function modify_existing_namespace($namespaces) {
if (isset($namespaces['wc/v3'])) {
$namespaces['wc/v3']['routes']['/custom-route'] = array(
'methods' => 'GET',
'callback' => 'custom_callback_function',
);
}
return $namespaces;
}
このサンプルでは、WooCommerceの既存の名前空間wc/v3に新しいルート/custom-routeを追加しています。このルートはGETリクエストに対して特定のコールバック関数を呼び出します。
引用元: https://woocommerce.com/document/woocommerce-rest-api/
サンプルコード3
add_filter('woocommerce_rest_api_get_rest_namespaces', 'remove_default_namespace');
function remove_default_namespace($namespaces) {
unset($namespaces['wc/v3']);
return $namespaces;
}
このコードは、WooCommerceのデフォルトの名前空間wc/v3を削除します。これにより、そのエンドポイントへアクセスできなくなります。
引用元: https://wordpress.stackexchange.com/questions/326659/how-to-add-woocommerce-custom-endpoint
サンプルコード4
add_filter('woocommerce_rest_api_get_rest_namespaces', 'add_custom_fields');
function add_custom_fields($namespaces) {
$namespaces['custom/v1'] = array(
'routes' => array(
'/custom-field' => array(
'methods' => 'POST',
'args' => array(
'custom_data' => array(
'required' => true,
'validate_callback' => function ($param) {
return !empty($param);
},
),
),
'callback' => 'custom_field_callback',
),
),
);
return $namespaces;
}
この例では、カスタムフィールドを持つPOSTリクエストのルート/custom-fieldを設定しています。このフィールドには値が必須で、バリデーションコールバック関数を使用して空でないことを確認しています。
引用元: https://nikschavan.com/adding-custom-endpoints-woocommerce-rest-api/
サンプルコード5
add_filter('woocommerce_rest_api_get_rest_namespaces', 'conditional_namespace');
function conditional_namespace($namespaces) {
if (is_user_logged_in()) {
$namespaces['user/v1'] = array(
'routes' => array(),
);
}
return $namespaces;
}
このサンプルは、ユーザーがログインしている場合のみuser/v1の名前空間を追加します。これにより、認証が必要な機能を持たせることができます。
引用元: https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-custom-endpoints/