创建自定义API端点,并将逻辑从PHP文件移到其中
例如,下面的代码将在中创建端点/wp-json/my/v1/video/<id>
add_action( \'rest_api_init\', function() {
register_rest_route( \'my/v1\', \'/video/(?P<id>\\d+)\', [
\'methods\' => \'GET\',
\'callback\' => \'get_video_data\',
\'permission_callback\' => \'__return_true\',
] );
} );
function get_video_data( $params ) {
$video_id = $params[\'id\'];
// do something like calling the DB where your video data is stored
// you can use custom post type to store the data
return $video_id;
}