Wordpress - 基于角色的注销重定向 [英] Wordpress - Logout redirection based on role

查看:55
本文介绍了Wordpress - 基于角色的注销重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to determine how I can redirect users upon logout, to a URL defined by their role. Simply put, I want to redirect admins (as well as editors) that logout to a different URL, than subscribers / privileged users.

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

I'm using the following code to redirect users at logout right now, but this redirects everyone. Any insight as to how I can have a different redirect based on their account role, would be great!

/**
 * 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' ) );

谢谢!

推荐答案

我不确定没有测试,但您可以使用带有 if else 语句的用户角色

Im not sure didnt test but you can use user roles with if else statement

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' ) );

这篇关于Wordpress - 基于角色的注销重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆