而docs state:
请注意,API不能阻止您更改响应,但代码的结构强烈阻止了这一点。内部,field registration is powered by filters, 和these can be used if you absolutely have no other choice.
那里IS 大多数情况下的另一种选择,即:custom endpoints/routes.
您甚至可以将rest控制器类分配给CPT。所以你可以扩展WP_Rest_Posts_Controller 和设置custom endpoints, their routes, 参数,以及相应的回调以响应您所需的任何内容。
涉及的步骤包括:
设置rest_controller_class
对于自定义帖子类型(CPT)
Extend WP_REST_Controller
或WP_REST_Posts_Controller
注册路由并定义方法可能使用模式控制响应格式注意:WP_REST_Posts_Controller
自身正在扩展WP_REST_Controller
. 设置rest_controller_class
CPT的参数:
1)In$args
注册时的数组: $labels = array( ... );
$args = array(
\'labels\' => $labels,
...
...
\'show_in_rest\' => true,
\'rest_base\' => \'my_rest_base\',
//\'rest_controller_class\' => \'WP_REST_Posts_Controller\',
\'rest_controller_class\' => \'My_CPT_Controller_Class\'
);
register_post_type( \'my-post-type\', $args );
2)注册CPT后要添加,请使用过滤器挂钩:register_post_type_args
function add_rest_stuff( $args, $post_type ) {
$custom_post_type = \'my-post-type\';
if ( $post_type !== $custom_post_type ) {
return $args;
}
$args[\'show_in_rest\'] = true;
$args[\'rest_base\'] = \'my_rest_base\';
$args[\'rest_controller_class\'] = \'My_CPT_Controller_Class\';
return $args;
}
add_filter(\'register_post_type_args\', \'make_it_public\' );
延伸WP_REST_Controller
对于自定义端点/路由:
作为起点的快速部分示例(从previous answer) class My_CPT_Controller_Class extends WP_REST_Controller {
public function __construct() {
add_action( \'rest_api_init\', array( $this, \'register_routes\' ) );
}//end __construct
public function register_routes() {
$version = \'1\';
$namespace = \'my-fancy-namespace/v\' . $version;
$base = \'my-route-base\';
// so, site.com/wp-json/my-fancy-namespace/v1/my-route-base/
register_rest_route( $namespace, \'/\'. $base, array(
array(
\'methods\' => \'GET\',
\'callback\' => array( $this, \'my_get_callback\' ),
\'permission_callback\' => array( $this, \'key_permissions_check\' ),
),
array(
\'methods\' => \'POST\',
\'callback\' => array( $this, \'my_post_callback\' ),
\'permission_callback\' => array( $this, \'key_permissions_check\' ),
),)
);
$base2 = \'my-second-base\';
// so, site.com/wp-json/my-fancy-namespace/v1/my-second-base/
register_rest_route( $namespace, \'/\'. $base2, array(
array(
\'methods\' => \'GET\',
\'callback\' => array( $this, \'my_get_callback_two\' ),
\'permission_callback\' => array( $this, \'key_permissions_check\' ),
),
array(
\'methods\' => \'POST\',
\'callback\' => array( $this, \'my_post_callback_two\' ),
\'permission_callback\' => array( $this, \'key_permissions_check\' ),
),)
);
}//register_routes
public function key_permissions_check() {
//do permissions check stuff
}
public function my_get_callback( WP_REST_Request $request ) {
//do stuff with $request
//see the methods mentioned below
}//end
}//end class
TheWP_Rest_Request class provides several methods 用于处理$request
. 我添加了prefix_get_comment()
该页底部的示例,因为这是一个简单的示例:
function prefix_register_my_comment_route() {
register_rest_route( \'my-namespace/v1\', \'/comments\', array(
// Notice how we are registering multiple endpoints the \'schema\' equates to an OPTIONS request.
array(
\'methods\' => \'GET\',
\'callback\' => \'prefix_get_comment_sample\',
),
// Register our schema callback.
\'schema\' => \'prefix_get_comment_schema\',
) );
}
/**
* Get our sample schema for comments.
*/
function prefix_get_comment_schema() {
$schema = array(
// This tells the spec of JSON Schema we are using which is draft 4.
\'$schema\' => \'http://json-schema.org/draft-04/schema#\',
// The title property marks the identity of the resource.
\'title\' => \'comment\',
\'type\' => \'object\',
// In JSON Schema you can specify object properties in the properties attribute.
\'properties\' => array(
\'id\' => array(
\'description\' => esc_html__( \'Unique identifier for the object.\', \'my-textdomain\' ),
\'type\' => \'integer\',
\'context\' => array( \'view\', \'edit\', \'embed\' ),
\'readonly\' => true,
),
\'author\' => array(
\'description\' => esc_html__( \'The id of the user object, if author was a user.\', \'my-textdomain\' ),
\'type\' => \'integer\',
),
\'content\' => array(
\'description\' => esc_html__( \'The content for the object.\', \'my-textdomain\' ),
\'type\' => \'string\',
),
),
);
return $schema;
}