Skip to content

Instantly share code, notes, and snippets.

@rodrisan
Forked from claudiosanches/plugin.php
Created May 24, 2016 20:33
Show Gist options
  • Save rodrisan/b3575c50f5e572300f277bb91acb82b5 to your computer and use it in GitHub Desktop.
Save rodrisan/b3575c50f5e572300f277bb91acb82b5 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