Last active
September 24, 2021 15:43
-
-
Save danielcharrua/d3459a80c7a194bfb8a41220d0306e1c to your computer and use it in GitHub Desktop.
Restrict permission on WooCommerce REST API
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 | |
/** | |
* 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