Self hosted wordpress/woocommerce and n8n on a private network

Scenario:
I have wordpress and n8n on vms in my privately hosted environment. I was trying to call an n8n workflow from woocommerce plugin on wordpresss when an order was created or a user was created and could not get it to work. There was no evidence that the webhook was reaching my n8n instance. I was getting error that the uri was invalid if I tried to setup a webhook manually or update the webhook that n8n created for the woocommerce node.

After much troubleshooting and research i found that woocommerce is using a wordpress function that validates the url. Not only does it validate the url though it does a dns lookup of the host to see if the ip address is a publicly routable ip and returns invalid if it is not.

Solution:
Had to add a filter in wordpress to allow my ip address of the server hosting my n8n instance. Below is the code to add to your theme’s function.php. I ended up creating a wordpress plugin from this with the help of chatgpt – read chatgpt built me a wordpress plugin from this.

function allow_specific_ips_for_webhooks( $is_external, $host, $url ) {
// Define the IPs or hosts you want to allow
$allowed_ips = array( ‘192.168.10.146’, ‘192.168.10.44’ ); // Add your allowed IPs here

// Try to resolve the host to an IP address
$ip = gethostbyname( $host );

// Check if the resolved IP is in the allowed IP list
if ( in_array( $ip, $allowed_ips ) ) {
    return true; // Allow this host/IP
}

// Otherwise, follow the normal WordPress validation
return $is_external;

}
add_filter( ‘http_request_host_is_external’, ‘allow_specific_ips_for_webhooks’, 10, 3 );

reference material - https://developer.wordpress.org/reference/functions/wp_http_validate_url/