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:

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:

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

Misha Rudrastyh

Hey guys and welcome to my website. For more than 10 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

Follow me on X