0

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/GeoIPDetecti‌​onv 76.php:6) in /home/server1/public_html/wp-content/mu-plugins/GeoIPDetecti‌​onv 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);
  • 2
    All mu plugins are loaded, they aren't conditionally loaded depending on which page you're on, what are you trying to do? – Tom J Nowell Nov 8 '17 at 1:35
  • Hi Tom, thanks for the reply. If I do not place it in the 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 on mywebsite.com/link, an American could be redirected to google.com while an Indian would be directed to amazon.com. – firefirehelphelp Nov 8 '17 at 4:42
  • While another PHP script would handle redirection for another page - so if customers land on www.website.com/link2, an American could be directed to youtube.com while an Indian would be directed to yahoo.com. May I know if this is clear? – firefirehelphelp Nov 8 '17 at 4:44
  • You should be using a single script instead with either if elseif condition or switch statement for differente page conditions – Kumar Nov 8 '17 at 5:03
  • Hi sven, there are over 15 pages on my website and each page redirects to different links so in total there would be an incredible number of links to manage. I wanted to split them into different PHP files so that it would be easier to manage. May I know if there is a way around this? Thanks. – firefirehelphelp Nov 8 '17 at 5:17
1

This is the example code to fix the fatal error and redirect user as per the given page id. You can further optimise the code, if the redirection is going to remain same for "DE" and "US" for all the pages, you can just skip the if ( is_page( 90 ) ) check for these country codes.

add_action( 'template_redirect', 'geoip_redirect', 5 );

function geoip_redirect() {

    //Return if not admin, or the blog id is not 123, or not on the specified page, or if the function doesn't exists
    //List of pages to redirect from
    $pages = array(
        50,
        90
    );
    //Return if not admin, or the blog id is not 123, or not on the specified page, or if the function doesn't exists
    if ( is_admin() || 123 !== get_current_blog_id() || ! is_page( $pages ) || ! function_exists( 'geoip_detect2_get_info_from_current_ip' ) ) {
        return;
    }

    $userInfo    = geoip_detect2_get_info_from_current_ip();
    $countryCode = $userInfo->country->isoCode;

    if ( is_page( 90 ) ) {
        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';
        }
    } elseif ( is_page( 50 ) ) {
        //Change URL accordingly
        switch ( $countryCode ) {
            case 'DE':
                $redirect_to = '/germany';
                break;
            case 'US':
                $redirect_to = '/usa';
                break;
            case 'SG':
                $redirect_to = 'www.amazon.com.sg';
                break;
            default:
                $redirect_to = 'www.amazon.com.sg';
        }
        //So on you can add additional else if conditions for rest of your pages
    }

    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;
    }
}

This code is suitable if redirection is gonna remain same for "US" and "DE"

add_action( 'template_redirect', 'geoip_redirect', 5 );

function geoip_redirect() {

    //List of pages to redirect from
    $pages = array(
        50,
        90
    );
    //Return if not admin, or the blog id is not 123, or not on the specified page, or if the function doesn't exists
    if ( is_admin() || 123 !== get_current_blog_id() || ! is_page( $pages ) || ! function_exists( 'geoip_detect2_get_info_from_current_ip' ) ) {
        return;
    }

    $userInfo    = geoip_detect2_get_info_from_current_ip();
    $countryCode = $userInfo->country->isoCode;

    if ( in_array( $countryCode, array( "US", "DE" ) ) ) {
        $redirect_to = 'US' == $countryCode ? '/usa' : '/germany';
    } else {
        if ( is_page( 90 ) ) {
            switch ( $countryCode ) {
                case 'SG':
                    $redirect_to = 'www.google.com.sg';
                    break;
                default:
                    $redirect_to = 'www.google.com.sg';
            }
        } elseif ( is_page( 50 ) ) {
            //Change URL accordingly
            switch ( $countryCode ) {
                case 'SG':
                    $redirect_to = 'www.amazon.com.sg';
                    break;
                default:
                    $redirect_to = 'www.amazon.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;
    }
}

Well, you can further optimise the code, depending upon what are the redirection values for your 15 pages, if it's all similar you can definitely come up with ways to minimise the code.

  • Hi sven, many thanks. I would try this when I get home. – firefirehelphelp Nov 8 '17 at 5:31
  • Just wondering, to shorten the code I am thinking of placing the case portion of each section into a separate php file and then use include to call them. Would this work? By case portion, I mean the following section 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'; – firefirehelphelp Nov 8 '17 at 5:56
  • Not really a good idea. By shortening the code I meant improving the logic. – Kumar Nov 8 '17 at 7:51
  • Thanks for the feedback sven. But if I do not do this, the entire script would be incredibly long and difficult to maintain. May I know what are the drawbacks of implementing what I mentioned? Or if you have another suggestion I would love to hear it. Thanks. – firefirehelphelp Nov 8 '17 at 8:06
  • I don't think script is going to be that long, but if you find it easier to divide the code into multiple files and include them go ahead. It's more of a personal choice, I'd rather prefer the related code in one place, unless it's going to be very long. Also, adding a file for each function is going to be tedious in general practice, which becomes even harder to maintain in long run. – Kumar Nov 8 '17 at 8:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.