rest_api_default_filters()
云策文档标注
概述
rest_api_default_filters() 函数用于注册 WordPress REST API 的默认过滤器,主要处理 REST 请求时的 CORS 头、允许头、响应字段过滤以及选项请求等。它通过附加到 'rest_api_init' 动作来简化测试和禁用这些过滤器。
关键要点
- 函数在 wp_is_serving_rest_request() 为真时注册废弃函数、参数和错误处理的钩子,以抑制相关错误报告。
- 默认注册多个过滤器,包括 rest_pre_serve_request、rest_post_dispatch 和 rest_pre_dispatch,用于处理 CORS 头、允许头、响应字段过滤和选项请求。
- 函数自 WordPress 4.4.0 版本引入,是 REST API 核心功能的一部分。
代码示例
function rest_api_default_filters() {
if ( wp_is_serving_rest_request() ) {
// Deprecated reporting.
add_action( 'deprecated_function_run', 'rest_handle_deprecated_function', 10, 3 );
add_filter( 'deprecated_function_trigger_error', '__return_false' );
add_action( 'deprecated_argument_run', 'rest_handle_deprecated_argument', 10, 3 );
add_filter( 'deprecated_argument_trigger_error', '__return_false' );
add_action( 'doing_it_wrong_run', 'rest_handle_doing_it_wrong', 10, 3 );
add_filter( 'doing_it_wrong_trigger_error', '__return_false' );
}
// Default serving.
add_filter( 'rest_pre_serve_request', 'rest_send_cors_headers' );
add_filter( 'rest_post_dispatch', 'rest_send_allow_header', 10, 3 );
add_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10, 3 );
add_filter( 'rest_pre_dispatch', 'rest_handle_options_request', 10, 3 );
add_filter( 'rest_index', 'rest_add_application_passwords_to_index' );
}
原文内容
Registers the default REST API filters.
Description
Attached to the ‘rest_api_init’ action to make testing and disabling these filters easier.
Source
function rest_api_default_filters() {
if ( wp_is_serving_rest_request() ) {
// Deprecated reporting.
add_action( 'deprecated_function_run', 'rest_handle_deprecated_function', 10, 3 );
add_filter( 'deprecated_function_trigger_error', '__return_false' );
add_action( 'deprecated_argument_run', 'rest_handle_deprecated_argument', 10, 3 );
add_filter( 'deprecated_argument_trigger_error', '__return_false' );
add_action( 'doing_it_wrong_run', 'rest_handle_doing_it_wrong', 10, 3 );
add_filter( 'doing_it_wrong_trigger_error', '__return_false' );
}
// Default serving.
add_filter( 'rest_pre_serve_request', 'rest_send_cors_headers' );
add_filter( 'rest_post_dispatch', 'rest_send_allow_header', 10, 3 );
add_filter( 'rest_post_dispatch', 'rest_filter_response_fields', 10, 3 );
add_filter( 'rest_pre_dispatch', 'rest_handle_options_request', 10, 3 );
add_filter( 'rest_index', 'rest_add_application_passwords_to_index' );
}
Changelog
| Version | Description |
|---|---|
| 4.4.0 | Introduced. |