根据页面的不同,通过注册重定向到不同的页面

时间:2017-03-14 作者:Kristoffer M

我的函数中有以下代码。php:

add_filter(\'foo_registration_redirect\', \'my_foo_registration_redirect\', 12);

function my_foo_registration_redirect( $redirect_to) {
    if (is_page(1693)) {
         $redirect_to = \'/my-page\';
         return $redirect_to;
    }
    return $redirect_to;
}
当我在相应的页面上时,if语句出于某种原因不是真的。我还尝试使用

 if (is_page(\'page-name\')) { ... }
尽管在正确的页面上,它也永远不会返回真值。非常感谢您的帮助。如果我将代码更改为

function my_foo_registration_redirect( $redirect_to) {
         $redirect_to = \'/my-page\';
         return $redirect_to; 
}
然而,我需要有条件的,因为我有2个登记表,在每个页面上,其中一个需要指向其他地方。

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

将函数更改为以下操作,因为它检查表单是否从正确的页面提交:

function my_foo_registration_redirect( $redirect_to) {
        if ($_SERVER[\'HTTP_REFERER\'] !== get_the_permalink(1693)) {
            return $redirect_to;
        }
        $redirect_to = \'/videoguide-startpakke\';
        return $redirect_to;
}
非常感谢您的宝贵意见!

编辑:这里有一个稍微更完善的解决方案

function my_foo_registration_redirect( $redirect_to) {
        $post_id = 1693;
        $post = get_post($post_id); 
        $slug = $post->post_name;

        if ($_SERVER[\'HTTP_REFERER\'] !== get_the_permalink($post_id)) {
            return $redirect_to;
        }
        $redirect_to = $slug;
        return $redirect_to;
}

相关推荐

无法在模板函数.php中使用IS_HOME

我试图在标题中加载一个滑块,但只在主页上加载。如果有帮助的话,我正在使用Ultralight模板。我正在尝试(在template functions.php中)执行以下操作:<?php if ( is_page( \'home\' ) ) : ?> dynamic_sidebar( \'Homepage Widget\' ); <?php endif; ?> 但这行不通。现在,通过快速的google,我似乎需要将请