• This is a modification to the code created by @areimann. It’s designed to check against an array of countries. This specific version is to check whether the user is in an area under GDPR and direct them to a “Location not available” page, because that’s something a lot of people may be looking to accomplish right now. It could easily be modified to direct users from a group of nations or locations to a different version of a website. Thanks, @areimann, for creating such useful code!

    /**
     * Redirect EU users to "website not available" site.
     */
    function wpe_country_code_redirect() {
    
        // Verify that the WPEngine Geo IP plugin is available.
        if ( ! class_exists( '\WPEngine\GeoIp' ) ) {
            return;
        }
    
        // EU country code
        $eu_country_code = array('AT', 'BE', 'BG', 'HR', 'CY', 'CZ', 'DK', 'EE', 'FI', 'FR', 'DE', 'GR', 'HU', 'IE', 'IT', 'LV', 'LT', 'LU', 'MT', 'NL', 'PL', 'PT', 'RO', 'SK', 'SI', 'ES', 'SE', 'GB');
    
        // URL to Location Unavailable page.
        $gdpr_url = 'https://www.mywebsite.com/location-unavailable/';
    
        // Instantiate GeoIp class.
        $geo = WPEngine\GeoIp::instance();
    
        // Get current country code.
        $user_country_code = $geo->country();
    
        // If the current user is accessing the website from the
        // domestic country code, then redirect to Location Unavailable page.
        if (in_array($user_country_code, $eu_country_code)){
            wp_redirect( $gdpr_url );
            exit;
        }
    }
    
    add_action( 'init', 'wpe_country_code_redirect' );
    
  • The topic ‘Code to redirect GDPR residents’ is closed to new replies.