Skip to content

Instantly share code, notes, and snippets.

@tomjn
Last active Aug 15, 2018
Embed
What would you like to do?
Visiting https://example.com/wp-json/tomjn/v1/addition?a=1&b=2 should return the response 3
<?php
function toms_addition_endpoint( \WP_REST_Request $request ) {
$result = $request['a'] + $request['b'];
return rest_ensure_response( $result );
}
function toms_addition_routes() {
register_rest_route( 'tomjn/v1', '/addition', array(
'methods' => WP_REST_Server::READABLE,
'callback' => 'toms_addition_endpoint',
'args' => [
'a' => [
'description' => 'the first number to add',
'type' => 'number',
'validate_callback' => 'is_numeric',
],
'b' => [
'description' => 'the second number to add',
'type' => 'number',
'validate_callback' => 'is_numeric',
],
],
));
}
add_action( 'rest_api_init', 'toms_addition_routes' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment