如果用户已登录,则重定向用户

时间:2020-04-26 作者:Patrick

下面的脚本非常有效,如果注销的用户试图访问“我的帐户”页面,它会将其重定向到登录页面

/**************************************START*******************************************
 *********** Redirect account page to login page if user is logged out ****************
**************************************************************************************/
add_action( \'template_redirect\', function() {

  if ( is_user_logged_in() || ! is_page() ) return;

  $logged_in_restricted = array( 5156, 000 ); // all your restricted pages if user is logged out

  if ( in_array( get_queried_object_id(), $logged_out_restricted ) ) {
    wp_redirect( site_url( \'/login\' ) );  // page user redirected to if restriction met
    exit();
  }

});
如何利用相同的脚本逻辑,但以登录用户为目标,而不是作为附加脚本注销?例如,如果登录用户试图进入登录/注册页面,现在我想将他们重定向到“我的帐户”页面。下面是一个示例:

/**************************************START*******************************************
 ******* Redirect login and register page to account page if user is logged In *******
**************************************************************************************/
add_action( \'template_redirect\', function() {

  if ( is_user_logged_out() || ! is_page() ) return;

  $logged_in_restricted = array( 3156, 4532 ); // all your restricted pages if user is logged In

  if ( in_array( get_queried_object_id(), $logged_in_restricted ) ) {
    wp_redirect( site_url( \'/my-account\' ) );  // page user redirected to if restriction met
    exit();
  }

});
我明白了is_user_logged_out 不是一件事。我可以用什么来实现这一点?

2 个回复
SO网友:simongcc

方法1可以使用login_redirect 用于添加登录重定向url的筛选器。

筛选器传递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_redirectlogin_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优化角度和个人经验。

SO网友:Patrick

我对上述逻辑非常满意,只需要稍微改变一下。添加! 之前is_user_logged_in() 完全解决了我的问题,并允许我反向执行逻辑。

/**********************************START*************************************
********** Redirect account page to login page if user is logged In *********
****************************************************************************/

add_action( \'template_redirect\', function() {

  if ( ! is_user_logged_in() || ! is_page() ) return;

  $restricted = array( 4062, 4066 ); // all your restricted pages

  if ( in_array( get_queried_object_id(), $restricted ) ) {
    wp_redirect( site_url( \'/account\' ) ); 
    exit();
  }

});

相关推荐

WooCommerce-TEMPLATE_REDIRECT如果结账且订单已付款?

在我的WooCommerce商店中,我想在付款完成后重定向到自定义的“感谢页面”。我正在使用以下代码:add_action( \'template_redirect\', \'myfunction\' ); function myfunction( $order_id ) { $order = wc_get_order( $order_id ); // I assume this is where I go wrong. Probably this is empty?&#