rest_get_server

函数
rest_get_server ( No parameters )
返回值
  • (WP_REST_Server) REST server instance.
定义位置
相关方法
wp_sitemaps_get_serverrest_get_url_prefixrest_get_avatar_urlsrest_get_route_for_postcreate_user
引入
4.5.0
弃用
-

rest_get_server: 这个函数用于检索当前的REST服务器实例。它返回WP_REST_Server的一个实例,它是负责处理REST API请求的类。

检索当前的REST服务器实例。

如果没有实例存在,则实例化一个新的实例。

function rest_get_server() {
	/* @var WP_REST_Server $wp_rest_server */
	global $wp_rest_server;

	if ( empty( $wp_rest_server ) ) {
		/**
		 * Filters the REST Server Class.
		 *
		 * This filter allows you to adjust the server class used by the REST API, using a
		 * different class to handle requests.
		 *
		 * @since 4.4.0
		 *
		 * @param string $class_name The name of the server class. Default 'WP_REST_Server'.
		 */
		$wp_rest_server_class = apply_filters( 'wp_rest_server_class', 'WP_REST_Server' );
		$wp_rest_server       = new $wp_rest_server_class;

		/**
		 * Fires when preparing to serve a REST API request.
		 *
		 * Endpoint objects should be created and register their hooks on this action rather
		 * than another action to ensure they're only loaded when needed.
		 *
		 * @since 4.4.0
		 *
		 * @param WP_REST_Server $wp_rest_server Server object.
		 */
		do_action( 'rest_api_init', $wp_rest_server );
	}

	return $wp_rest_server;
}

常见问题

FAQs
查看更多 >