基于用户角色的 Woo Commerce 登录重定向

MDL*_*MDL 5 php wordpress woocommerce

我正在寻找一种方法来重定向我的批发用户。

我的 woo-commerce 设置用于批发的方式是根据我命名为“批发”的 WordPress 用户角色将用户分配给批发。

我想保留普通用户的重定向而不进行更改,但添加一种将该批发角色发送到另一个服装页面的方法。

我使用了 Peters Login Redirect 和 WordPress Login Redirect,尽管它们具有该功能,但这两个插件都不起作用 - 为自定义页面输入正确的设置后的结果是默认的 woo commerce 我的帐户页面。

有没有办法通过functions.php 做到这一点?

hel*_*ing 1

您可以直接从Codex使用Peter 的登录重定向或此示例。只需切换到或无论您的角色被称为什么。administratorwholesale

/**
 * Redirect user after successful login.
 *
 * @param string $redirect_to URL to redirect to.
 * @param string $request URL the user is coming from.
 * @param object $user Logged user's data.
 * @return string
 */
function my_login_redirect( $redirect_to, $request, $user ) {
    //is there a user to check?
    global $user;
    if ( isset( $user->roles ) && is_array( $user->roles ) ) {
        //check for admins
        if ( in_array( 'administrator', $user->roles ) ) {
            // redirect them to the default place
            return $redirect_to;
        } else {
            return home_url();
        }
    } else {
        return $redirect_to;
    }
}

add_filter( 'login_redirect', 'my_login_redirect', 10, 3 );
Run Code Online (Sandbox Code Playgroud)

修改后的 Codex 示例将具有wholesale角色的用户发送到自定义页面:

/**
 * Redirect wholesalers to custom page user after successful login.
 *
 * @param string $redirect_to URL to redirect to.
 * @param string $request URL the user is coming from.
 * @param object $user Logged user's data.
 * @return string
 */
function my_login_redirect( $redirect_to, $request, $user ) {
    //is there a user to check?
    global $user;
    if ( isset( $user->roles ) && is_array( $user->roles ) ) {
        //check for admins
        if ( in_array( 'wholesale', $user->roles ) ) {
            $redirect_to = 'http://example.com/wholesale';
        } 
    } 
    return $redirect_to;
}

add_filter( 'login_redirect', 'my_login_redirect', 10, 3 );
Run Code Online (Sandbox Code Playgroud)