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

Pass String Parameters to WP 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',
    ) );
} );

知道如何传递字符串吗?

我自己找的...

对字符串使用 [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',
    ) );
} );

也尝试使用下面的代码来定义端点..

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

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

你需要试试这个,它会起作用

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