我的函数中有以下代码。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个登记表,在每个页面上,其中一个需要指向其他地方。
最合适的回答,由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;
}