[WordPress]RestAPI#2 エンドポイント追加

functions.php

/**
 * RestAPIエンドポイント追加
 */
function custom_register_rest_routes() {
    /* カテゴリ一覧 */
    register_rest_route('custom/v1', '/categories/', [
        'methods'  => 'GET',
        'callback' => 'custom_callback_categories',
        'permission_callback' => '__return_true' // 認証不要
    ]);
}
add_action('rest_api_init', 'custom_register_rest_routes');

/**
 * カテゴリ一覧取得
 *
 * @return [type]
 * 
 */
function custom_callback_categories($request) {
    $wpt = new KtyrReport2();
    return new WP_REST_Response([
        'terms' => $wpt->get_terms(),
    ], 200);
}