Skip to content

Instantly share code, notes, and snippets.

@claudiosanches
Created January 29, 2016 22:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save claudiosanches/24380108940011017e6c to your computer and use it in GitHub Desktop.
Save claudiosanches/24380108940011017e6c to your computer and use it in GitHub Desktop.
WordPress - Custom REST API endpoint example
<?php
add_action( 'rest_api_init', function() {
register_rest_route( 'my-route/v1', '/products/(?P<id>\d+)', array(
'methods' => 'GET',
'callback' => function( $data ) {
$product = wc_get_product( $data['id'] );
if ( empty( $product ) ) {
return null;
}
return $product;
},
'permission_callback' => function() {
return current_user_can( 'edit_products' );
}
) );
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment