Skip to content

Instantly share code, notes, and snippets.

@tomjn
Last active August 15, 2018 22:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomjn/6f8d63cc72a5a9f5414d79cf7934da1e to your computer and use it in GitHub Desktop.
Save tomjn/6f8d63cc72a5a9f5414d79cf7934da1e to your computer and use it in GitHub Desktop.
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