Last active
August 15, 2018 22:08
-
-
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
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
<?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