Skip to content

Instantly share code, notes, and snippets.

@andrasguseo
Last active November 7, 2024 22:00
Show Gist options
  • Save andrasguseo/7e06d64dced302e056cfea386f3c1e6d to your computer and use it in GitHub Desktop.
Save andrasguseo/7e06d64dced302e056cfea386f3c1e6d to your computer and use it in GitHub Desktop.
TEC > Block REST API requests coming from EA.
<?php
/**
* Block REST API requests coming from Event Aggregator. Basically blocks imports.
* It can block other sites trying to siphon events with EA through the "Other URL" source.
*
* Usage: Add the snippet with a plugin like Code Snippets or to your functions.php file.
*
* @author: Andras Guseo
*
* Plugins required: The Events Calendar
*
* @since November 7, 2024 Initial version.
*/
add_filter( 'rest_pre_dispatch', function ( $result, $server, $request ) {
// List of IPs to block
$blocked_ips = [ '199.189.224.106' ];
// Get the client IP address
$client_ip = $_SERVER['REMOTE_ADDR'];
// Check if this is a request to the events endpoint
if ( $request->get_route() === '/tribe/events/v1/events' ) {
// If the client IP is in the blocked list, deny access
if ( in_array( $client_ip, $blocked_ips ) ) {
return new WP_Error(
'rest_forbidden',
__( 'Access to this endpoint is blocked.' ),
array( 'status' => 403 )
);
}
}
return $result;
}, 10, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment