Skip to content

Instantly share code, notes, and snippets.

@matthewstokeley
Last active July 19, 2019 16:48
Show Gist options
  • Save matthewstokeley/71ba613f5a17a425355a29a9aff211cf to your computer and use it in GitHub Desktop.
Save matthewstokeley/71ba613f5a17a425355a29a9aff211cf to your computer and use it in GitHub Desktop.
add a menu endpoint to the wordpress rest api
/**
*
* @param array $data options
* @return array|null an array of links
*/
function find_menu( WP_REST_Request $data ) {
$menu = wp_get_nav_menu_items( $data['name'] );
if ( empty( $menu ) ) {
return rest_ensure_response(new WP_Error( 'no menu', 'Invalid menu', array( 'status' => 404 )));
}
// @todo
// $response = new WP_REST_Response( $menu );
// $response->set_status( 201 );
// $response->header( 'Location', 'http://google.com' );
return rest_ensure_response($menu);
}
add_action( 'rest_api_init', function() {
register_rest_route( 'find/v1', '/menu', array(
'methods' => 'GET',
'callback' => 'find_menu',
'args' => array(
'name' => array(
'validate_callback' => function( $param, $request, $key ) {
return is_string( $param );
},
//'sanitize_callback' => 'absint'
)
)
) );
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment