duanqian2368 2019-04-04 17:46
浏览 70

基于WP_Query的页面重定向

I'm using a custom query parameter in Word, named responses for some dynamic content. I've added some conditionals that redirect a user back to the root URL if the response is put in incorrectly so they can't access a blank page. What am I doing wrong here, and is there a cleaner way to do it?

functions.php:

function add_query_vars_filter($vars) { 
  $vars[] = "response"; 
  return $vars; 
}
add_action("query_vars", "add_query_vars_filter");

The code below does work, but it looks messy to me and doesn't seem very scalable for more conditionals. page.php:

$response = get_query_var('response');

if (!$response) { 
  header("Location: https://" . $_SERVER['HTTP_HOST']);
} 
if ($response == "yes" || $response == "no") { 
  header("Location: https://" . $_SERVER['HTTP_HOST']);
}

Ideally, I'd prefer to have it all in a single conditional statement, however when I write:


if (!$response || $response == "yes" || $response == "no") { 
  header("Location: https://" . $_SERVER['HTTP_HOST']); 
}

But that doesn't redirect the user back to the host when they type ?response=yes or ?response=no

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 sub地址DHCP问题
    • ¥15 delta降尺度计算的一些细节,有偿
    • ¥15 Arduino红外遥控代码有问题
    • ¥15 数值计算离散正交多项式
    • ¥30 数值计算均差系数编程
    • ¥15 redis-full-check比较 两个集群的数据出错
    • ¥15 Matlab编程问题
    • ¥15 训练的多模态特征融合模型准确度很低怎么办
    • ¥15 kylin启动报错log4j类冲突
    • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大