简体   繁体   中英

Pass String Parameters to WP REST API

I can pass integer values to WP REST API. But, cannot pass non-numeric characters. It gives error.

This is what I used...

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

Any idea how to pass strings as well.. ?

I found it myself...

use [a-zA-Z0-9-] instead of \\d for strings

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

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

Try below code for define endpoint as well..

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

you need to try this, it will work

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM