
如何在WordPress中实现按类别搜索
rest_handle_options_request ( $response, $handler, $request )
rest_handle_options_request: 这是一个WordPress的函数,用于处理一个REST API端点的HTTP OPTIONS请求: 该函数在HTTP允许头中为端点发送允许的HTTP方法,以及该端点支持的任何额外的头。
处理服务器的OPTIONS请求。
这是在服务器代码之外处理的,因为它不服从正常的路由映射。
function rest_handle_options_request( $response, $handler, $request ) { if ( ! empty( $response ) || $request->get_method() !== 'OPTIONS' ) { return $response; } $response = new WP_REST_Response(); $data = array(); foreach ( $handler->get_routes() as $route => $endpoints ) { $match = preg_match( '@^' . $route . '$@i', $request->get_route(), $matches ); if ( ! $match ) { continue; } $args = array(); foreach ( $matches as $param => $value ) { if ( ! is_int( $param ) ) { $args[ $param ] = $value; } } foreach ( $endpoints as $endpoint ) { // Remove the redundant preg_match() argument. unset( $args[0] ); $request->set_url_params( $args ); $request->set_attributes( $endpoint ); } $data = $handler->get_data_for_route( $route, $endpoints, 'help' ); $response->set_matched_route( $route ); break; } $response->set_data( $data ); return $response; }