admin管理员组

文章数量:1423156

I would like to add a REST route that would looks like

.../v1/import/?url=XXX

where XXX is an URL.

I had a try with

register_rest_route('.../v1','import/?url=(?:/(?P<url>\w+))?', array(
        array(
            'methods'             => WP_REST_Server::READABLE,
            'callback'            => array( $this, 'import_url' ),
            'args' => array(
                'url' => array(
                    //'validate_callback' => array($this, 'validateImportUrl')
                ),
            ),
        )
    ) );

but it does not work:

{"code":"rest_no_route","message":"No route was found matching the URL and request method","data":{"status":404}}

Can anyone help ?

Thanks !

I would like to add a REST route that would looks like

.../v1/import/?url=XXX

where XXX is an URL.

I had a try with

register_rest_route('.../v1','import/?url=(?:/(?P<url>\w+))?', array(
        array(
            'methods'             => WP_REST_Server::READABLE,
            'callback'            => array( $this, 'import_url' ),
            'args' => array(
                'url' => array(
                    //'validate_callback' => array($this, 'validateImportUrl')
                ),
            ),
        )
    ) );

but it does not work:

{"code":"rest_no_route","message":"No route was found matching the URL and request method","data":{"status":404}}

Can anyone help ?

Thanks !

Share Improve this question asked Jun 19, 2019 at 19:23 gordiegordie 4925 silver badges19 bronze badges 1
  • Query parameters don't belong in the route, they should be added as arguments, which you've already done. – Jacob Peattie Commented Jun 20, 2019 at 1:40
Add a comment  | 

1 Answer 1

Reset to default -1

Regex problem, I found out :)

register_rest_route( '.../v1', 'import(?:/?url=(?P<url>\d+))?', array(

本文标签: how can I add an URL parameter to a rest route using registerrestroute()