<?php
/**
* Template Name: Custom Page Template
*/
get_header();
if(is_user_logged_in()){
// content for login user
}
else {
// https://developer.wordpress.org/reference/functions/wp_redirect/
// wp_redirect() does not validate that the $location is a reference to the current host
// That means this function is vulnerable to open redirects if you pass it a $location supplied by the user.
// So use it only when navigating to another website
wp_safe_redirect(\'/wp-login.php\');
exit;
}
?>
Wp_reDirect()在自定义模板文件中不起作用
3 个回复
最合适的回答,由SO网友:Rarst 整理而成
重定向是通过输出HTTP头来执行的,wp_redirect()
只需在其上添加一些位,以实现灵活性。
标题仅在任何和所有输出到页面之前使用,因为这是HTTP响应的结构。
假设它可以在模板中工作,如果您确保它在任何输出之前启动。实际上,在到达任何模板/输出之前,通常在适当的挂钩上处理重定向。常用的钩子是template_redirect
.
SO网友:Wh0CaREs
据我所知,您可以在将内容发送到浏览器之前使用wp\\u重定向。你应该用钩子。
但是您可以使用PHP头函数
if ( TRUE ) {
header( "Location: " . home_url() );
die();
}
但我不能百分之百确定这个标题(位置)是否会让你对“标题已经发送”产生问题SO网友:Dev02ali
try this code,
get_header();
global $current_user;
get_currentuserinfo();
if(is_user_logged_in()){
// content for login user
}
else {
$url = \'http://example.com\';
wp_redirect($url);
exit();
}
结束