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

rest api - Adding Wordpress API Endpoint With Multiple Parameters

programmeradmin1浏览0评论

So I understand for the most part how the wp rest controller works, what it is doing and why it is the best way to do it. The trouble I am having is wrapping my head around the regular expressions in the endpoint URL for the function register_rest_route.

Regular expressions are what they are but I was wondering if someone could break it down for me in this context.

Some example code

register_rest_route( $this->namespace, '/' . $this->resource_name . '/(?P<id>[\d]+)', array(
        // Notice how we are registering multiple endpoints the 'schema' equates to an OPTIONS request.
        array(
            'methods'   => 'GET',
            'callback'  => array( $this, 'get_item' ),
            'permission_callback' => array( $this, 'get_item_permissions_check' ),
        ),
        // Register our schema callback.
        'schema' => array( $this, 'get_item_schema' ),
    ) );

So the (?P<id>[\d]+) confuses me a bit I understand it means that the parameter of id is required but what if I wanted multiple parameters and what if wanted to have a route that was something like /vendor/v1/geolocate/{param}/{param} or /vender/v1/?id={param}&address={param}

So I understand for the most part how the wp rest controller works, what it is doing and why it is the best way to do it. The trouble I am having is wrapping my head around the regular expressions in the endpoint URL for the function register_rest_route.

Regular expressions are what they are but I was wondering if someone could break it down for me in this context.

Some example code

register_rest_route( $this->namespace, '/' . $this->resource_name . '/(?P<id>[\d]+)', array(
        // Notice how we are registering multiple endpoints the 'schema' equates to an OPTIONS request.
        array(
            'methods'   => 'GET',
            'callback'  => array( $this, 'get_item' ),
            'permission_callback' => array( $this, 'get_item_permissions_check' ),
        ),
        // Register our schema callback.
        'schema' => array( $this, 'get_item_schema' ),
    ) );

So the (?P<id>[\d]+) confuses me a bit I understand it means that the parameter of id is required but what if I wanted multiple parameters and what if wanted to have a route that was something like /vendor/v1/geolocate/{param}/{param} or /vender/v1/?id={param}&address={param}

Share Improve this question edited Mar 13, 2018 at 20:39 mmm 3,8193 gold badges16 silver badges22 bronze badges asked Mar 13, 2018 at 20:32 Aaron BlakeleyAaron Blakeley 1681 gold badge1 silver badge3 bronze badges 6
  • 4 the regex part ([\d]+) means "a decimal part at least 1 time" and adding ?P<id> give the name "id" to this argument instead of just calling im "1" (as first argument). then you need something like /(?P<id>[\d]+)/(?P<address>[^/]+) – mmm Commented Mar 13, 2018 at 20:45
  • Would you recommend a particular piece of reading material to catch up on this? I have avoided regex for years and limped by on online code snippets. Perhaps it is time to put it to bed. By bed, I mean actually learn it. – Aaron Blakeley Commented Mar 13, 2018 at 20:49
  • One last question I also saw a regular expression like this /users/market=(?P<market>[a-zA-Z0-9-]+)/lat=(?P<lat>[a-z0-9 .\-]+)/long=(?P<long>[a-z0-9 .\-]+) Would this be called like /users/market={param}/lat={param}/long={param} – Aaron Blakeley Commented Mar 13, 2018 at 20:56
  • An easy way to test regex is by using a site like regex101. Enter your regex on top and then your test string underneath. On the right side you'll see the matching strings/groups. – swissspidy Commented Mar 13, 2018 at 21:55
  • @mmm very good explanation. – maheshwaghmare Commented Mar 17, 2018 at 20:32
 |  Show 1 more comment

1 Answer 1

Reset to default 3

I have the same problem, i search on google finally with the help of the above answer i found out the solution. this may help others.

$this->base = home
register_rest_route( 

        $namespace, '/' . $this->base . '/' .  'products' . '/', array(
            array( 
                'methods'   => WP_REST_Server::READABLE, 
                'callback'  => array( $this, 'rest_api_popular_products'), 
            ),
        )  
    );        
    register_rest_route(
        $namespace, '/' . $this->base . '/' .  'products' . '/(?P<category>[\d]+)/(?P<sort>[\w]+)', array(
            'args'   => array(
                'id' => array(
                    'description' => __( 'Unique identifier for the resource.', 'woocommerce' ),
                    'type'        => 'integer',
                ),
            ),
            array(
                'methods'             => WP_REST_Server::READABLE,
                'callback' => array( $this, 'rest_api_popular_products' ),                  
                'args'                => array(
                    'context' => $this->get_context_param( array( 'default' => 'view' ) ),
                ),
            )
        )
    );

API request like : ..wp-json/wc/v2/home/products/?category=89&sort=popularity

发布评论

评论列表(0)

  1. 暂无评论