Documentation
GeoBlock
Developer Reference

Developer Reference

GeoBlock exposes filters and constants that developers can use to extend or modify its behaviour.


Constants

ConstantValueDescription
GEOBLOCK_VERSION'1.0.0'Plugin version string
GEOBLOCK_PLUGIN_FILEabsolute pathMain plugin file path
GEOBLOCK_PLUGIN_DIRabsolute pathPlugin directory path (with trailing slash)
GEOBLOCK_PLUGIN_URLURLPlugin directory URL (with trailing slash)
GEOBLOCK_PLUGIN_BASErelative pathPlugin basename (e.g. windcodex-geoblock/windcodex-geoblock.php)

Filters

geoblock_resolve_product_id

Allows external code to resolve a product ID before GeoBlock reads its restriction rules. Used internally by the WPML compatibility layer to map translated product IDs back to the original.

/**
 * @param int $product_id  The product ID being checked.
 * @return int             The resolved product ID (e.g. the original language ID).
 */
add_filter( 'geoblock_resolve_product_id', function( int $product_id ): int {
    // Return a different ID if needed.
    return $product_id;
} );

woocommerce_product_is_visible (standard WC filter)

GeoBlock hooks into this filter at priority 10 to hide products in Hide and Catalog Only modes. You can hook at a higher priority to override GeoBlock's decision:

add_filter( 'woocommerce_product_is_visible', function( bool $visible, int $product_id ): bool {
    // Your custom logic here.
    return $visible;
}, 20, 2 );

Options

GeoBlock stores its global settings in a single WordPress option.

Option name: geoblock_settings

array(
    'restriction_mode'    => 'hide',          // 'hide' | 'catalog_only' | 'message'
    'message_position'    => 'after_title',   // 'before_title' | 'after_title' | 'after_price' | 'after_cart'
    'force_geolocation'   => 'no',            // 'yes' | 'no'
    'redirect_enabled'    => 'no',            // 'yes' | 'no'
    'redirect_url'        => '',              // URL string or empty
    'custom_message'      => 'Sorry, ...',    // HTML-allowed string
    'catalog_purchasable' => 'no',            // 'yes' | 'no'
    'debug_mode'          => 'no',            // 'yes' | 'no'
)

Reading settings in your own code:

$settings = (array) get_option( 'geoblock_settings', array() );
$mode     = $settings['restriction_mode'] ?? 'hide';

Per-Product Meta

Restriction rules are stored as post meta on each product.

Meta key: _geoblock_rules

array(
    'mode'      => 'exclude',                       // 'include' | 'exclude'
    'countries' => array( 'US', 'CA', 'DE' ),       // Array of 2-letter ISO codes (uppercase)
)

Reading product rules:

$rules     = get_post_meta( $product_id, '_geoblock_rules', true );
$mode      = $rules['mode']      ?? 'exclude';
$countries = $rules['countries'] ?? array();

When the country list is empty, the meta key is deleted entirely rather than stored as an empty array. Always check empty( $rules ) before using the result.


Geolocation API

You can instantiate GeoBlock_Geolocation directly to detect the current visitor's country in your own code (after GeoBlock has loaded):

$geo     = new GeoBlock_Geolocation();
$country = $geo->get_country(); // Returns 2-letter ISO code, e.g. 'DE'
$ip      = $geo->get_ip();      // Returns the visitor's public IP address

To manually override the detected country for the current session:

$geo->set_country( 'FR' ); // Sets country to France for this session

Restriction Engine API

$geo          = new GeoBlock_Geolocation();
$restrictions = new GeoBlock_Restrictions( $geo );
 
// Check if a product is restricted for the current visitor.
$is_restricted = $restrictions->is_restricted( $product_id ); // bool
 
// Get the global restriction mode.
$mode = $restrictions->get_restriction_mode(); // 'hide' | 'catalog_only' | 'message'
 
// Get the custom restriction message.
$message = $restrictions->get_restriction_message(); // string (HTML-safe)
 
// Get the redirect URL.
$url = $restrictions->get_redirect_url(); // URL string or empty
 
// Get raw rules for a product.
$rules = $restrictions->get_product_rules( $product_id ); // array
 
// Save rules for a product.
$restrictions->save_product_rules( $product_id, array(
    'mode'      => 'exclude',
    'countries' => array( 'US', 'CA' ),
) );