cagartner
7/18/2016 - 3:14 AM

WordPress - Custom REST API endpoint example

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' );
		}
	) );
} );