How to Create a Language Redirect in Multisite
Recently I got a request from my client and what she was needed is to automatically redirect users to a language-specific store within the WordPress multisite network depending on a customer IP address.
For example:
no.example.com– redirect customers here if they have a norwegian IP address,dk.example.com– redirect customers here if they have a danish IP address,example.com– let customers to stay on this website if there is no specific language website version for them (based on their IP again).
What is the easiest way to accomplish it?
Let’s start it simple and just redirect users to a language-specific sub-store homepage. But below you will find a more interesting example where we’re redirecting customers to the same products (the products with the same SKUs).
add_action( 'template_redirect', 'rudr_multisite_redirect_by_lang' );
function rudr_multisite_redirect_by_lang() {
// WooCommerce is not installed
if( ! class_exists( 'WC_Geolocation' ) ) {
return;
}
// Multisite is not installed
if( ! function_exists( 'get_site' ) ) {
return;
}
// Customer IP information
$location = WC_Geolocation::geolocate_ip();
if( ! isset( $location[ 'country' ] ) || ! $location[ 'country' ] ) {
return;
}
// current site information
$site = get_site();
// check if the detected country code matches the subdomain
if( str_starts_with( $site->domain, $location[ 'country' ] ) ) {
return;
}
$network = get_network();
if( get_blog_id_from_url( "{$location[ 'country' ]}.{$network->domain}" ) ) {
wp_redirect( "https://{$location[ 'country' ]}.{$network->domain}" );
exit;
}
}Please keep in mind the following:
- First of all, if you don’t know where to use this code, read this.
- Since we’re talking about a WooCommerce store, I am going to use
WC_Geolocationclass to detect a customer country by an IP address, but doing it on every page refresh doesn’t seem like a very good idea, so in the second example we will add PHP sessions to it. If you don’t use WooCommerce, you can use any external service API to detect a location by IP. - Using
function_exists( 'get_site' )is highly recommended because if you run this code outside a multisite network, you will get a fatal error. - If you don’t use PHP 8.0 or above, don’t forget to replace
str_starts_with()function withstrpos()orsubstr(). - And the last but not the least, with the help of
get_blog_id_from_url()we’re checking if a language-specific store exists and if so, redirect to it.
Now let’s apply all of this to a more complex example.
Redirect to Products with the Same SKUs
add_action( 'template_redirect', 'rudr_multisite_redirect_by_lang' );
function rudr_multisite_redirect_by_lang() {
// we are going to store the user country in PHP Sessions
session_start();
// WooCommerce is not installed
if( ! class_exists( 'WC_Geolocation' ) ) {
return;
}
// Performing a redirect only from product pages
if( ! is_singular( 'product' ) ) {
return;
}
// If product doesn't have a SKU, exit the function
$product = wc_get_product( get_queried_object_id() );
if( ! $product->get_sku() ) {
return;
}
// let's try to get the current user location either from sessions or from WC_Geolocation
if( ! isset( $_SESSION[ 'rudr_country_code' ] ) || ! $_SESSION[ 'rudr_country_code' ] ) {
$location = WC_Geolocation::geolocate_ip();
$_SESSION[ 'rudr_country_code' ] = isset( $location[ 'country' ] ) ? strtolower( $location[ 'country' ] ) : null;
}
if( ! $_SESSION[ 'rudr_country_code' ] ) {
return;
}
// get current site
$site = get_site();
// let's check if we're currently on the appropriate website
if( str_starts_with( $site->domain, $_SESSION[ 'rudr_country_code' ] ) ) {
return;
}
// we need to check whether there is a product with the same SKU on another country site
$network = get_network();
// get blog ID
$blog_id = get_blog_id_from_url( "{$_SESSION[ 'rudr_country_code' ]}.{$network->domain}" );
// there is no sub-store for the given language, exit the function
if( ! $blog_id ) {
return;
}
switch_to_blog( $blog_id );
if( function_exists( 'wc_get_product_id_by_sku' ) && ( $id = wc_get_product_id_by_sku( $product->get_sku() ) ) ) {
wp_redirect( get_permalink( $id ) );
exit;
}
restore_current_blog();
}If you have any questions about this code, please let me know in the comments.
Misha Rudrastyh
Hey guys and welcome to my website. For more than 15 years I've been doing my best to share with you some superb WordPress guides and tips for free.
Need some developer help? Contact me