Last active
August 28, 2016 07:58
-
-
Save websupporter/a107db8811ac0561f97d5ecd4b58a154 to your computer and use it in GitHub Desktop.
How to register an endpoint in the WordPress REST API #wcfra
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 | |
add_action( 'rest_api_init', 'my_own_restroute' ); | |
function my_own_restroute() { | |
register_rest_route( | |
'my-namespace/v1', | |
'/route/', | |
array( | |
'methods' => 'GET', | |
'callback' => 'my_restroute_callback', | |
'args' => array( | |
'id' => array( | |
'validate_callback' => function($param, $request, $key) { | |
return is_numeric( $param ); | |
}, | |
'sanitize_callback' => function($param, $request, $key) { | |
return (int) $param; | |
}, | |
'required' => true, | |
//'default' => 1, | |
'description' => 'The ID of the object.', | |
), | |
), | |
) | |
); | |
} | |
function my_restroute_callback( $request ) { | |
$data['id'] = $request['id']; | |
return $data; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment