将字符串参数传递给WP REST API

时间:2017-01-19 10:20:09

标签: php wordpress rest wordpress-rest-api

我可以将整数值传递给WP REST API。但是,不能传递非数字字符。它给出了错误。

这就是我用过的......

add_action( 'rest_api_init', function () {
    register_rest_route( 'crowdapi/v1', '/register/(?P<id>\d+)/(?P<username>\d+)', array(
        'methods' => 'POST',
        'callback' => 'userCheck',
    ) );
} );

还知道如何传递字符串..?

3 个答案:

答案 0 :(得分:13)

我自己找到了......

使用[a-zA-Z0-9-]代替\d代码

add_action( 'rest_api_init', function () {
    register_rest_route( 'crowdapi/v1', '/register/(?P<id>\d+)/(?P<number>[a-zA-Z0-9-]+)', array(
        'methods' => 'POST',
        'callback' => 'userCheck',
    ) );
} );

答案 1 :(得分:2)

这对我有用:/(?P<slug>\w+)

答案 2 :(得分:1)

尝试以下代码定义端点..

add_action( 'rest_api_init', function () {
    register_rest_route( 'crowdapi/v1', '/register/(?P<id>\d)/(?P<username>\d)', array(
        'methods' => 'POST',
        'callback' => 'userCheck',
    ) );
} );