Skip to content

Instantly share code, notes, and snippets.

@websupporter
Last active August 28, 2016 07:58
Show Gist options
  • Save websupporter/a107db8811ac0561f97d5ecd4b58a154 to your computer and use it in GitHub Desktop.
Save websupporter/a107db8811ac0561f97d5ecd4b58a154 to your computer and use it in GitHub Desktop.
How to register an endpoint in the WordPress REST API #wcfra
<?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