Skip to content

Instantly share code, notes, and snippets.

@damiencarbery
Last active November 18, 2021 18:44
Show Gist options
  • Save damiencarbery/a66fd27f6e2c33fdefc46d3a6e216b58 to your computer and use it in GitHub Desktop.
Save damiencarbery/a66fd27f6e2c33fdefc46d3a6e216b58 to your computer and use it in GitHub Desktop.
Disable WooCommerce REST API authentication: Override WooCommerce capability check so that all REST API queries are allowed. https://www.damiencarbery.com/2019/07/disable-woocommerce-rest-api-authentication/
<?php
/*
Plugin Name: Disable WooCommerce REST API authentication
Plugin URI: https://www.damiencarbery.com/2019/07/disable-woocommerce-rest-api-authentication/
Description: Override WooCommerce capability check so that all REST API queries from specified IP are allowed
Author: Damien Carbery
Version: 0.1
*/
add_filter( 'woocommerce_rest_check_permissions', 'dcwd_allow_rest_api_queries', 10, 4 );
function dcwd_allow_rest_api_queries( $permission, $context, $zero, $object ) {
// Optionally limit permitted queries to different contexts.
/*if ( 'read' != $context ) {
return $permission;
}*/
// Write the parameters to the error log (or debug.log file) to see what requests are being accessed.
//error_log( sprintf( 'Permission: %s, Context: %s; Object: %s', var_export( $permission, true ), $context, var_export( $object, true ) ) );
// Disable authentication for specific IP addresses.
$permitted_ips = array('12.34.56.78', '87.65.43.21');
if (in_array($_SERVER['REMOTE_ADDR'], $permitted_ips)) {
return true; // Allow all queries.
}
return $permission;
}
<?php
/*
Plugin Name: Disable WooCommerce REST API authentication
Plugin URI: https://www.damiencarbery.com/2019/07/disable-woocommerce-rest-api-authentication/
Description: Override WooCommerce capability check so that all REST API queries are allowed.
Author: Damien Carbery
Version: 0.1
*/
add_filter( 'woocommerce_rest_check_permissions', 'dcwd_allow_rest_api_queries', 10, 4 );
function dcwd_allow_rest_api_queries( $permission, $context, $zero, $object ) {
// Optionally limit permitted queries to different contexts.
/*if ( 'read' != $context ) {
return $permission;
}*/
// Write the parameters to the error log (or debug.log file) to see what requests are being accessed.
//error_log( sprintf( 'Permission: %s, Context: %s; Object: %s', var_export( $permission, true ), $context, var_export( $object, true ) ) );
return true; // Allow all queries.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment