2017-01-19 34 views
2

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', 
    )); 
}); 

だけでなく、文字列を渡す方法任意のアイデア..?

答えて

4

は私が..

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

文字列を

...それを使用[a-zA-Z0-9-]の代わり\dを自分自身を発見しました:/(?P<slug>\w+)

+0

私は '(?P 。+)'を使用しました。文字列パラメータの '(?P [^ /] +)'となります。それは本当にあなたが戻ってくるだけでなく、取り込む必要があるかにもよります。 – MrMesees

2

これは私のために働いたとしても、エンドポイントを定義するためのコードの下に試してみてください

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', 
    )); 
}); 
関連する問題