筛选器传递3个参数
// refer to /wp-login.php
/**
* Filters the login redirect URL.
*
* @since 3.0.0
*
* @param string $redirect_to The redirect destination URL.
* @param string $requested_redirect_to The requested redirect destination URL passed as a parameter.
* @param WP_User|WP_Error $user WP_User object if login was successful, WP_Error object otherwise.
*/
下面的示例只传递url,您还可以传递$user参数,并根据不同的用户进行重定向。
add_filter( \'login_redirect\', \'ws365094_logout_redirect_to\', 10, 3 );
function ws365094_logout_redirect_to( $redirect_to_url, $requested_redirect_to, $user ) {
// any additional logic
return site_url( \'/my-account\' ); // just pass this url, after login, it will be redirected by WordPress original login routine
}
方法2如果您有一个自定义的登录表单,您可以传递变量“redirect\\u to”,因为wp登录。php将检查是否有。如果将所有内容重定向到相同的url,则此方法比方法1简单。
<input type="hidden" name="redirect_to" value="somewhere_url" />
对于最初的注销方法,您还可以使用
logout_redirect 筛选器以添加注销重定向url,而不是
template_redirect 行动
template_redirect 非常普遍,但在重定向到特定模板(模板控件)时有更多用例。只是一个补充说明供参考。
之间的序列比较template_redirect 和login_redirect
根据加载顺序。默认情况下,登录后,WordPress会将用户重定向到特定页面,以使cookie正常工作,我已经回答了这个问题
here 之前
Login -> Redirect -> Template Redirect(canonical) which is normally done by redirect_canonical() if slug does not found, go to 404, if using /?p=111, check if permalink is in used, if so, direct to permalink structured url and so on
如果使用
template_redirect, 这意味着页面正在中处理
template-loader.php. 它变成
Login -> Redirect -> Template Redirect(canonical) -> Custom Template Redirect
因为它遵循模板加载器中的do\\u操作(“template\\u redirect”),在这种情况下,模板加载器将多次加载。优化脚本的一种方法是对自定义脚本使用更高的优先级
template_include 钩子以便它检查和重定向早期的功能,类似于登录重定向。
自到达后template_redirect 装载时间有点晚。因此,它仍然不是进行用户登录检查并重定向到与模板无关的地方的理想场所。
所以,除非有必要,如进行重写、自定义URL结构或自定义模板准备。不建议以后使用。此外,如果重定向处理不当,也可能会影响SEO排名。这是基于实际加载顺序、SEO优化角度和个人经验。