【问题标题】:Wordpress - Logout redirection based on roleWordpress - 基于角色的注销重定向
【发布时间】:2016-05-23 23:08:50
【问题描述】:

我正在尝试确定如何在用户注销时将其重定向到由他们的角色定义的 URL。简而言之,我想将注销的管理员(以及编辑)重定向到与订阅者/特权用户不同的 URL。

我现在正在使用以下代码在注销时重定向用户,但这会重定向每个人。任何关于我如何根据他们的帐户角色进行不同重定向的见解都会很棒!

/**
 * Redirect to custom login page after the user has been logged out.
*/
public function redirect_after_logout() {
    $redirect_url = home_url( 'member-login?logged_out=true' );
    wp_safe_redirect( $redirect_url );
    exit;
}

add_action( 'wp_logout', array( $this, 'redirect_after_logout' ) );

谢谢!

【问题讨论】:

    标签: php wordpress redirect


    【解决方案1】:

    我不确定没有测试,但您可以将用户角色与 if else 语句一起使用

    function redirect_after_logout() {
        if (!current_user_can('manage_options')) { 
            $url = '/';
        } else { 
            $url = 'member-login?logged_out=true';
        }
        $redirect_url = home_url( $url );
        wp_safe_redirect( $redirect_url );
        exit;
    }
    add_action( 'wp_logout', array( $this, 'redirect_after_logout' ) );
    

    【讨论】:

      猜你喜欢
      • 2014-04-09
      • 1970-01-01
      • 2013-08-31
      • 2020-09-17
      • 2014-03-26
      • 2019-12-12
      • 2020-03-26
      • 2020-09-07
      • 2019-05-26
      相关资源
      最近更新 更多