钩子文档

rest_api_init

💡 云策文档标注

概述

rest_api_init 是一个 WordPress 动作钩子,在准备处理 REST API 请求时触发。它用于注册自定义 REST 路由和端点,确保仅在需要时加载相关代码。

关键要点

  • 触发时机:在准备服务 REST API 请求时触发,用于初始化 REST API 相关功能。
  • 参数:接收一个参数 $wp_rest_server,类型为 WP_REST_Server,代表 REST API 服务器对象。
  • 最佳实践:建议在此钩子上创建端点对象并注册钩子,以避免不必要的加载,提高性能。
  • 版本历史:从 WordPress 4.4.0 版本开始引入。

代码示例

/**
 * 示例:使用 rest_api_init 注册自定义 REST 路由。
 *
 * 回调函数接收 WP_REST_Server 实例作为其唯一参数。
 *
 * @param WP_REST_Server $wp_rest_server REST API 服务器。
 */
function sample_register_rest_routes( WP_REST_Server $wp_rest_server ) {
    register_rest_route(
        'sample/v1',
        '/hello',
        array(
            'methods'             => WP_REST_Server::READABLE,
            'callback'            => 'sample_rest_hello_callback',
            'permission_callback' => '__return_true',
        )
    );
}
add_action( 'rest_api_init', 'sample_register_rest_routes', 10, 1 );

/**
 * /sample/v1/hello 路由的回调函数。
 *
 * @return array
 */
function sample_rest_hello_callback() {
    return array(
        'message' => 'Hello from the REST API!',
        'time'    => current_time( 'mysql' ),
    );
}

📄 原文内容

Fires when preparing to serve a REST API request.

Description

Endpoint objects should be created and register their hooks on this action rather than another action to ensure they’re only loaded when needed.

Parameters

$wp_rest_serverWP_REST_Server
Server object.

Source

do_action( 'rest_api_init', $wp_rest_server );

Changelog

Version Description
4.4.0 Introduced.

User Contributed Notes

  1. Skip to note 3 content

    /**
    * Example: Register a custom REST route using rest_api_init.
    *
    * The callback receives the WP_REST_Server instance as its single parameter.
    *
    * @param WP_REST_Server $wp_rest_server The REST API server.
    */
    function sample_register_rest_routes( WP_REST_Server $wp_rest_server ) {
    register_rest_route(
    ‘sample/v1’,
    ‘/hello’,
    array(
    ‘methods’ => WP_REST_Server::READABLE,
    ‘callback’ => ‘sample_rest_hello_callback’,
    ‘permission_callback’ => ‘__return_true’,
    )
    );
    }
    add_action( ‘rest_api_init’, ‘sample_register_rest_routes’, 10, 1 );

    /**
    * Callback for the /sample/v1/hello route.
    *
    * @return array
    */
    function sample_rest_hello_callback() {
    return array(
    ‘message’ => ‘Hello from the REST API!’,
    ‘time’ => current_time( ‘mysql’ ),
    );
    }

  2. Skip to note 4 content

    <br />
    /**<br />
    * Register a custom REST API route using the rest_api_init action.<br />
    *<br />
    * The callback receives the WP_REST_Server instance.<br />
    *<br />
    * @param WP_REST_Server $wp_rest_server The REST API server instance.<br />
    */
    <br />
    function sample_register_rest_routes( WP_REST_Server $wp_rest_server ) {</p>
    <p> register_rest_route(<br />
    'sample/v1',<br />
    '/hello',<br />
    array(<br />
    'methods' => WP_REST_Server::READABLE,<br />
    'callback' => 'sample_rest_hello_callback',<br />
    'permission_callback' => '__return_true',<br />
    )<br />
    );<br />
    }<br />
    add_action( 'rest_api_init', 'sample_register_rest_routes', 10, 1 );</p>
    <p>/**<br />
    * Callback for the /wp-json/sample/v1/hello endpoint.<br />
    *<br />
    * @return array<br />
    */
    <br />
    function sample_rest_hello_callback() {<br />
    return array(<br />
    'message' => 'Hello from the REST API!',<br />
    'time' => current_time( 'mysql' ),<br />
    );<br />
    }</p>
    <p>