I have multiple php scripts that sits in mu-plugins
folder to help redirect traffic. The php script is shown below and each script just differs in terms on the page ID eg. is_page( 90 )
for the first script and is_page( 50 )
for another script. However, I get the following error message
Fatal error: Cannot redeclare geoip_redirect() (previously declared in /home/server1/public_html/wp-content/mu-plugins/GeoIPDetectionv 76.php:6) in /home/server1/public_html/wp-content/mu-plugins/GeoIPDetectionv 90.php on line 43
May I know how to resolve this error? I tried include the declaration portion in core.php
(shown at bottom of post) and getting each script to include it only once (using include_once
) but am still getting the error. Thank you.
Main script
<?php /* Template Name: GeoIPDetectionv3 */
include_once 'core.php';
function geoip_redirect(){
if ( is_admin() ) {
return; // Not applicable.
}
if ( 123 !== get_current_blog_id() ) {
return; // Not on blog ID 123.
}
if ( ! is_page( 90 ) ) {
return; // Not a specific page ID on this blog.
}
if ( ! function_exists( 'geoip_detect2_get_info_from_current_ip' ) ) {
return;
}
$userInfo = geoip_detect2_get_info_from_current_ip();
$countryCode = $userInfo->country->isoCode;
switch ($countryCode) {
case 'DE':
$redirect_to = '/germany';
break;
case 'US':
$redirect_to = '/usa';
break;
case 'SG':
$redirect_to = 'www.google.com.sg';
break;
default:
$redirect_to = 'www.google.com.sg';
}
if ( ! empty( $redirect_to ) ) {
if ( stripos( $redirect_to, 'http' ) === 0 ) {
wp_redirect( $redirect_to ); // Full URL.
} else {
wp_redirect( home_url( $redirect_to ) ); // Local /path.
}
exit;
}
}
Core.php
<?php /* Template Name: GeoIPDetectionv3 */
add_action('template_redirect', 'geoip_redirect', 5);
mu-plugins
folder then where should I place it? The purpose of having multiple Php scripts is to redirect customers to different websites depending on the country they come from. Each script would handle redirection for one page - so if a customer lands onmywebsite.com/link
, an American could be redirected to google.com while an Indian would be directed to amazon.com.if elseif
condition orswitch
statement for differente page conditions