我有一个wordpress网站,在使用登录后,我希望它们重定向到不同的网站。我怎么能在wordpress中这样做?设置php标头函数(标题(“位置:http://www.somesite.com”))不起作用,它说标题已经在header.php文件中设置。所以基本上我如何通过wordpress重定向?
wordpress是否有自己的直接功能,我可以用它来安全地从wordpress网站重定向?我不知道还能做什么,所以请帮助我,谢谢。
答案 0 :(得分:3)
在普通网页中,您可以使用wp_redirect
(请参阅Function Reference/wp_redirect)
<?php
wp_redirect( $location, $status );
exit;
?>
要允许重定向到其他网站,请将以下内容添加到functions.php
(用您的值替换“其他”):
function my_allowed_redirect_hosts($allowed) {
$allowed[] = 'other.com';
$allowed[] = 'www.other.com';
return $allowed;
}
add_filter('allowed_redirect_hosts','my_allowed_redirect_hosts');
通常,如果登录页面的URL上有redirect_to
个查询字符串值,它将在验证后尝试重定向到该位置。
要更改登录将重定向用户的位置,而不考虑redirect_to
查询字符串值,请再次添加到functions.php
(用您的值替换位置):
function custom_login_redirect() {
return 'http://www.other.com/Home/Authenticated';
}
add_filter('login_redirect', 'custom_login_redirect');
要注销并重定向到其他网站,您可以使用以下内容:
<a href="<?=wp_logout_url( "http://other.com/Account/LogOff" )?>">Log Off</a>
答案 1 :(得分:0)
试试这个
//chage the redirection url for login
add_filter('login_redirect','my_redirect_to_my_site',100,3);
function my_redirect_to_my_site($redirect_to_calculated,$redirect_url_specified,$user){
return "http://google.com";//where you want to redirect ,change with that
}
//add the domain to allowed hosts list for redirection
add_filter('allowed_redirect_hosts','my_allowed_redirect_hosts');
function my_allowed_redirect_hosts($allowed_hosts){
$allowed_hosts[]='google.com'; //add the other domain to allowed hosts where to redirect
return $allowed_hosts;
}
确保添加主机名(您要在allowed_hosts列表中重定向的位置)。您可以将此代码放在主题的functiions.php中,它将在登录时重定向用户。 希望有所帮助:)