Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.. ?

like image 991
Tharindu Thisarasinghe Avatar asked Jan 19 '17 10:01

Tharindu Thisarasinghe


1 Answers

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',
    ) );
} );
like image 50
Tharindu Thisarasinghe Avatar answered Oct 12 '22 03:10

Tharindu Thisarasinghe