WooCommerce登录到特定页面或推荐人

时间:2017-03-17 作者:Danny

我设置了一个修改后的Woocommerce登录表单,以便在本博客的帮助下使用我正在使用的会员插件(付费会员Pro):Redirect after logging in.

   function wc_custom_user_redirect( $redirect, $user ) {
    // Get the first of all the roles assigned to the user
global $current_user;

    $level = pmpro_getMembershipLevelForUser($user->ID);
    $myaccount = get_permalink( wc_get_page_id( \'myaccount\' ) );

if          ( $level->id == 2 ) {
            $redirect = \'/rookie-dashboard/\';
} elseif    ( $level->id == 3 ) {
            $redirect = \'/player-dashboard/\';
} elseif    ( $level->id == 4 ) {
            $redirect = \'/all-star-dashboard/\';
} elseif    ( $level->id == 5 ) {
            $redirect = \'/coach-dashboard/\';
} elseif    ( $level->id == 6 ) {
            $redirect = \'/owner-dashboard/\';
} elseif    ( $level->id == 7 ) {
            $redirect = \'https://corporate.example.com/dashboard/\';
} elseif    ( $level->id == 8 ) {
            $redirect = \'/dashboard/\';

} else {
    $redirect = $myaccount;
}
    return $redirect;
}
add_filter( \'woocommerce_login_redirect\', \'wc_custom_user_redirect\', 10, 2 );
然而,我想知道是否有可能为这个函数添加一个推荐人。基本上,我希望函数首先检查是否请求了特定页面,如果是这样,那么他们将被重定向到/成员登录/因为站点被锁定,一旦登录,它将转到他们最初请求的特定页面。如果用户没有请求特定的页面,那么他们将被重定向到相应的仪表板,就像我上面所说的那样。

这是原始代码:

$redirect = wp_get_referer() ? wp_get_referer() : home_url();
然而,我不知道如何应用它。

谢谢

1 个回复
最合适的回答,由SO网友:Danny 整理而成

所以解决方案其实很简单。

首先,我需要修改我的自定义成员登录重定向,以便在重定向URL中包含redirect\\u to部分:

function pb4r_redirect()
{
    global $current_user;
    $refer=urlencode($_SERVER["REQUEST_URI"]);
    $okay_pages = array(4, 10, 12, 13, 191, 200, 1380, 1381, 1382, 1383, 1449, 1631, 1643, 1652, 1653, 1654, 1666, 1718, 1725, 1840, \'Checkout\', \'Cart\', pmpro_getOption(\'billing_page_id\'), pmpro_getOption(\'account_page_id\'), pmpro_getOption(\'levels_page_id\'), pmpro_getOption(\'checkout_page_id\'), pmpro_getOption(\'confirmation_page_id\'));

   //if the user is logged out

 if(!$current_user->ID
     && !is_home()
     && !is_page($okay_pages)
     && !strpos($_SERVER[\'REQUEST_URI\'], "login")
     && !is_singular( \'post\' )
     && !is_product()
    )
    {
        wp_redirect( \'/member-login\' . \'?redirect_to=\' . $refer );
        exit;
    } 
}
add_action(\'template_redirect\', \'pb4r_redirect\');
然后,我修改了woocommerce函数,将redirect\\u包含到:

function wc_custom_user_redirect( $redirect, $user ) {
    // Get the first of all the roles assigned to the user
global $current_user;

    $level = pmpro_getMembershipLevelForUser($user->ID);
    $myaccount = get_permalink( wc_get_page_id( \'myaccount\' ) );

if( isset( $_REQUEST[\'redirect_to\'] ) ) {
        return $_REQUEST[\'redirect_to\'];
}elseif      ( $level->id == 2 ) {
            $redirect = \'/rookie-dashboard/\';
} elseif    ( $level->id == 3 ) {
            $redirect = \'/player-dashboard/\';
} elseif    ( $level->id == 4 ) {
            $redirect = \'/all-star-dashboard/\';
} elseif    ( $level->id == 5 ) {
            $redirect = \'/coach-dashboard/\';
} elseif    ( $level->id == 6 ) {
            $redirect = \'/owner-dashboard/\';
} elseif    ( $level->id == 7 ) {
            $redirect = \'https://corporate.example.com/dashboard/\';
} elseif    ( $level->id == 8 ) {
            $redirect = \'/dashboard/\';

} else {
    $redirect = $myaccount;
}
    return $redirect;
}
add_filter( \'woocommerce_login_redirect\', \'wc_custom_user_redirect\', 10, 2 );
现在我可以将用户重定向到请求的页面。如果他们没有请求页面,则会自动重定向到仪表板。如果他们是注册用户,但不是会员计划的一部分,则会重定向到“我的帐户”页面。

相关推荐

WP_REDIRECT导致无限循环

Goal: 我修改了permalink,以显示特定类别的类别名称:grill rezepte。默认情况下,永久链接为:https://website.com/post-title 现在只有烧烤餐厅:https://website.com/grill-rezepte/post-title因此,我想在所有grill Recipes上设置301重定向,以便它们移动到新的URL。我有下面的代码(见下文),它会导致一个无限循环。Code:add_action(\'template_redirect\', \'pos