WordPress Rest API自定义路由,正则匹配纯整数和字符串

WordPress Rest API自定义路由参数,常用正则匹配:

  • 纯整数:/(?P[\d]+)
  • 字符串:/(?P[a-zA-Z0-9-]+)

示例:

add_action( 'rest_api_init', 'custom_rest_register' );

function custom_rest_register() {
	register_rest_route( 'nrd/v1', 'teams/(?P<id>[\d]+)', [
	    'methods'  => 'GET',
	    'callback' => 'custom_rest_register_a'
	  ] );

	register_rest_route( 'nrd/v1', 'groups/(?P<id>[a-zA-Z0-9-]+)', [
	    'methods'  => 'GET',
	    'callback' => 'custom_rest_register_b'
	  ] );
}

function custom_rest_register_a($request) {
}

function custom_rest_register_a($request) {
}

(版权归cpury.com所有,转载请注明出处。)