致命错误:无法重新声明函数

时间:2017-11-08 作者:firefirehelphelp

我有多个php脚本mu-plugins 用于帮助重定向流量的文件夹。php脚本如下所示,每个脚本在页面ID上都有所不同,例如。is_page( 90 ) 对于第一个脚本和is_page( 50 ) 另一个脚本。但是,我收到以下错误消息

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

我可以知道如何解决这个错误吗?我试着在core.php (显示在文章底部)并使每个脚本只包含一次(使用include_once) 但我仍然得到了错误。非常感谢。

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

1 个回复
最合适的回答,由SO网友:Kumar 整理而成

这是修复致命错误并根据给定页面id重定向用户的示例代码。您可以进一步优化代码,如果所有页面的“DE”和“US”重定向保持不变,您可以跳过if ( is_page( 90 ) ) 检查这些国家/地区代码。

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;
    }
}
如果重定向对“US”和“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;
    }
}
好吧,您可以进一步优化代码,具体取决于15页的重定向值是多少,如果它们都相似的话,您肯定可以想出方法来最小化代码。

结束