Skip to content

Instantly share code, notes, and snippets.

@danielcharrua
Last active September 24, 2021 15:43
Show Gist options
  • Save danielcharrua/d3459a80c7a194bfb8a41220d0306e1c to your computer and use it in GitHub Desktop.
Save danielcharrua/d3459a80c7a194bfb8a41220d0306e1c to your computer and use it in GitHub Desktop.
Restrict permission on WooCommerce REST API
<?php
/**
* Restrict permission on WooCommerce REST API based on user_login and request type
* Let 'daniel' access ONLY to products data
*/
add_filter( 'woocommerce_rest_check_permissions', 'charrua_allow_only_products', 10, 4 );
function charrua_allow_only_products( $permission, $context, $object_id, $type ) {
if ('daniel' === wp_get_current_user()->user_login){
return ( 'product' === $type ) ? $permission : false;
}
return $permission;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment