Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PHP文件中使用wordpress函数_Php_Wordpress - Fatal编程技术网

在PHP文件中使用wordpress函数

在PHP文件中使用wordpress函数,php,wordpress,Php,Wordpress,我有一个WordPress子主题,我使用一个php文件作为特定页面的模板 我正在尝试为名为GeoIP检测的插件实现一个API。请参阅下面我在网站上使用的PHP文件。我尝试应用的API是“根据国家重定向” 当我加载脚本时,我应该被重定向到https://www.google.com.sg但是,它不这样做 多谢各位 我的PHP <?php /* Template Name: GeoIPDetectionv3 */ add_action('template_redirect', 'geoip_

我有一个WordPress子主题,我使用一个php文件作为特定页面的模板

我正在尝试为名为GeoIP检测的插件实现一个API。请参阅下面我在网站上使用的PHP文件。我尝试应用的API是“根据国家重定向”

当我加载脚本时,我应该被重定向到
https://www.google.com.sg
但是,它不这样做

多谢各位

我的PHP

<?php /* Template Name: GeoIPDetectionv3 */

add_action('template_redirect', 'geoip_redirect', 5);
function geoip_redirect(){
    if (is_admin())
        return;

    // This condition prevents a redirect loop:
    // Redirect only if the home page is called. Change this condition to the specific page or URL you need.
    if (!is_page(90))
        return;

    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':
            $url = '/germany';
            break;
        case 'US':
            $url = '/usa';
            break;
        case 'SG':
            $url = 'https://www.google.com.sg';
            break;
        default:
            $url = 'https://www.google.com.sg';
    }
    if ($url) {
        wp_redirect(get_current_blog_id(null, $url));
        exit;
    }
}

使用单个PHP标记,并确保代码的最后一部分实际上位于PHP标记内。目前它不是,所以它被解析为纯文本

更新:我已经为您整理了一下,并更新了代码以反映您修改后的问题;i、 e.以下是我们的评论

<?php /* Template Name: GeoIPDetectionv3 */

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

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 = 'https://www.google.com.sg';
            break;
        default:
            $redirect_to = 'https://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;
    }
}

听起来像是一个问题。输出不是错误,而是实际的text@Praveen库马尔,我也会在那里碰碰运气谢谢。嗨,贾斯沃克斯,谢谢你的回复。我不能删除脚本的
部分,否则wordpress将不允许我选择它作为页面的模板。此外,我无法将PHP文件放在MU插件文件夹中,因为我使用的是子主题,并且根据此网站(
www.wpneyer.com/wp themes/how-to-create-a-custom-page-in-wordpress/
),我需要将PHP文件放在这里
/wp content/themes/directory
。尽管如此,我还是尝试了上面的脚本,将其放在
/wp content/themes/目录中,并直接运行它,得到一个空白页。@firehlphelp我看到了。是的,我忘了在那里添加模板名称注释。我刚刚编辑,将其添加到我为您发布的示例中。@FireHelpHelp它可以放在您喜欢的任何地方。只需确保将所有PHP代码封装在PHP标记中即可。我建议使用一个MU插件文件,但也可以使用一个主题。话虽如此,您发布的原始示例在
template\u重定向
hook之前需要
wp load.php
。这不起作用,因为
wp load.php
不会触发
template\u重定向
wp load.php
是一种只加载WordPress框架而不加载模板输出的特殊方式。您好,jaswrks,谢谢。我已将该文件放在mu plugins文件夹中,并删除了对
wp load.php
的引用。你能帮我做以下(1)修改后的代码是在我原来的帖子中指出的。但是,re direct函数的工作原理很奇怪。我应该被重定向到
https://www.google.com.sg
但是我被引导到
http://mywebsite.com/https://www.google.com.sg
。(2) 您能解释一下为什么需要将它放在
mu plugins
文件夹中吗?我看了你贴的链接,不知道为什么。PHP文件不是插件。最后,将PHP文件放在mu插件文件夹中会影响我的所有其他站点。是否有可能使其仅影响一个特定站点?非常感谢。