-
-
Save cagartner/b7a587b46be44254d22220f0e76e0157 to your computer and use it in GitHub Desktop.
WordPress - Custom REST API endpoint example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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