I was searching the WP REST Documentation and have been googling around but I haven’t seen some examples using GET method with multiple variable so I just played around with the code and URL and way able to get integer values when using this code in your custom plugin or functions.php:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| add_action( 'rest_api_init', function () { | |
| register_rest_route( | |
| 'testapi/v1', '/getvars/(?P<var1>\d+)/(?P<var2>\d+)', array( | |
| 'methods' => 'GET', | |
| 'callback' => 'wp_restapi_getvars', | |
| ) | |
| ); | |
| } | |
| ); | |
| function wp_restapi_getvars( $data ) { | |
| echo 'This is var1:' . $data['var1'] . '<br>'; | |
| echo 'This is var2:' . $data['var2'] . '<br>'; | |
| } |
And when you try this example URL :
This will give you an output of:
This is var1:123
This is var2:456
I think I need to dig and explore more to expand its usage especially on other data types other than integers
