最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

validation - How to define a query parameter with REST API?

programmeradmin4浏览0评论

I'm trying to define a GET REST API endpoint with register_rest_route

register_rest_route supports defining a URL parameter and it is documented at

I would love to define a list of query parameters and let Wordpress handle the validation part. Is there an official way to do so?

I'm trying to define a GET REST API endpoint with register_rest_route

register_rest_route supports defining a URL parameter and it is documented at https://developer.wordpress/rest-api/extending-the-rest-api/adding-custom-endpoints/#arguments

I would love to define a list of query parameters and let Wordpress handle the validation part. Is there an official way to do so?

Share Improve this question asked Jul 9, 2020 at 10:52 왕뚜껑왕뚜껑 651 silver badge3 bronze badges 5
  • 1 Yes, you linked to it. What do you need help with? You mention WordPress 'handling the validation', but it can't do that unless you tell it how, which is what the validate_callback is for. – Jacob Peattie Commented Jul 9, 2020 at 10:59
  • @JacobPeattie The link explains about how to define an URL parameter (/author/(?P<id>\d+)). I would like to define a query parameter (ex: ?id=test) – 왕뚜껑 Commented Jul 9, 2020 at 11:01
  • They're defined the same way, in the args argument. By using regex in the endpoint you can accept them as part of the URL, but by default they're passed as query parameters. – Jacob Peattie Commented Jul 9, 2020 at 11:11
  • @JacobPeattie Ah! I did not know that. Thank you so much. – 왕뚜껑 Commented Jul 9, 2020 at 11:12
  • 1 Can you post the answer as an answer? – Tom J Nowell Commented Jul 9, 2020 at 11:29
Add a comment  | 

1 Answer 1

Reset to default 3

Yes, from the link you posted there are examples for both sanitising and validating the parameters. Validating does a check which can fail and block the API call from running; sanitising just does some operations to clean or interpret the parameter and does not stop the API call from running.

An example of validation, taken from the page:

<?php
add_action( 'rest_api_init', function () {
  register_rest_route( 'myplugin/v1', '/author/(?P<id>\d+)', array(
    'methods' => 'GET',
    'callback' => 'my_awesome_func',
    'args' => array(
      'id' => array(
        'validate_callback' => function($param, $request, $key) {
          return is_numeric( $param );
        }
      ),
    ),
  ) );
} );

You can see in the second parameter of register_rest_route that this defines an endpoints like /author/1234

The format of the second parameter of register_rest_route breaks down as:

  • /author/ initial part of the URL to match
  • ?P a code specific to this function that means 'parameter'. Note this is not included in the URL when called
  • <id> optional name for the parameter, used belows in args, not included as part of URL.
  • \d+ the regex for this parameter
发布评论

评论列表(0)

  1. 暂无评论