ワードプレスのregister_rest_routeアクションの使用方法・解説

概要

register_rest_routeアクションは、WordPressのREST APIに新しいエンドポイントを登録するために使用されます。この関数を利用することで、開発者は特定のルートに対してカスタム処理を定義し、APIを介してデータの取得や保存を簡単に行うことができます。以下は、このアクションがよく使われる機能の例です:

  1. カスタムポストタイプのデータ取得
  2. プラグインの設定データの保存
  3. 外部サービスとのデータ連携
  4. ユーザー情報の管理
  5. アプリケーションのための特定のデータフィルタリング
  6. カスタムフィールドの操作
  7. フロントエンドアプリケーションにデータを提供
  8. Ajaxリクエストの処理

構文

register_rest_route( $namespace, $route, $args );

パラメータ

  • $namespace(string): ルートをグループ化するための名前空間。
  • $route(string): ルートのエンドポイント。
  • $args(array): ルートに対するオプションやコールバック関数。

戻り値

  • boolean : 成功した場合にtrueを、失敗した場合にfalseを返します。

関連する関数

使用可能なバージョン

  • WordPress 4.7以降で利用可能です。

コアファイルのパス

  • wp-includes/rest-api.php

この関数のアクションでの使用可能性

アクション 使用例
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: カスタムポストタイプの取得

このコードは、特定のカスタムポストタイプのデータを取得するためのREST APIエンドポイントを登録しています。

add_action('rest_api_init', function () {
    register_rest_route('custom/v1', '/posts/', array(
        'methods' => 'GET',
        'callback' => 'get_custom_posts',
    ));
});

function get_custom_posts() {
    $posts = get_posts(array('post_type' => 'custom_post'));
    return $posts;
}

(出典: https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-custom-endpoints/)

サンプル2: データの保存

このコードでは、クライアントから送信されたデータを保存するためのエンドポイントを作成しています。

add_action('rest_api_init', function () {
    register_rest_route('custom/v1', '/save/', array(
        'methods' => 'POST',
        'callback' => 'save_custom_data',
    ));
});

function save_custom_data(WP_REST_Request $request) {
    $data = $request->get_param('data');
    // データの保存処理
    return new WP_REST_Response('Data saved', 200);
}

(出典: https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-custom-endpoints/)

サンプル3: ユーザー情報の取得

このコードは、現在のユーザーの情報をREST API経由で取得するためのエンドポイントを作成しています。

add_action('rest_api_init', function () {
    register_rest_route('custom/v1', '/user/', array(
        'methods' => 'GET',
        'callback' => 'get_current_user_info',
    ));
});

function get_current_user_info() {
    return wp_get_current_user();
}

(出典: https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-custom-endpoints/)

サンプル4: フィルタリングされたデータの取得

このコードは、特定の条件に基づいた投稿を取得するエンドポイントを登録します。

add_action('rest_api_init', function () {
    register_rest_route('custom/v1', '/filtered-posts/', array(
        'methods' => 'GET',
        'callback' => 'get_filtered_posts',
    ));
});

function get_filtered_posts(WP_REST_Request $request) {
    $category = $request->get_param('category');
    $posts = get_posts(array(
        'category_name' => $category
    ));
    return $posts;
}

(出典: https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-custom-endpoints/)

サンプル5: カスタムフィールドの操作

このコードは、カスタムフィールドを取得するREST APIエンドポイントを作成しています。

add_action('rest_api_init', function () {
    register_rest_route('custom/v1', '/custom-field/', array(
        'methods' => 'GET',
        'callback' => 'get_custom_field_data',
    ));
});

function get_custom_field_data($data) {
    $post_id = $data['id'];
    $custom_field = get_post_meta($post_id, 'custom_field_key', true);
    return $custom_field;
}

(出典: https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-custom-endpoints/)

この関数について質問する


上の計算式の答えを入力してください