停止WooCommerce将wp-login.php和wp-admin重定向到账户页面

3

使用普通的WordPress安装,订阅者可以访问/wp-login.php,登录并访问控制面板。

我发现在安装WooCommerce后,如果订阅者已登录,然后尝试重新访问wp-admin或wp-login.php,则会被重定向到在WooCommerce设置中设置的“我的帐户”页面。

我想知道是否有任何方法可以删除此功能,因为它不适用于我的网站。

非常感谢任何想法。

解决方案

我已经找到了解决方案,并在相关问题上发布了它。

2个回答

4
这里有一种方法可以实现“订阅者”用户角色的操作:
// Conditional function code for 'subscriber' User Role
function is_subscriber_user(){
    if( current_user_can('subscriber') ) return true;
    else return false;
 }

// Redirect 'subscriber' User Role to the User edit prodile on WooCommerce's My Account
// So when he get looged or it register too
 add_filter('template_redirect', 'wp_subscriber_my_account_redirect' );
function wp_subscriber_my_account_redirect() {
    if( is_subscriber_user() && is_account_page() )
        wp_redirect( get_edit_profile_url( get_current_user_id() ) );
}

// Prevent automatic woocommerce redirection for 'subscriber' User Role 
add_filter( 'woocommerce_prevent_automatic_wizard_redirect', 'wc_subscriber_auto_redirect', 20, 1 );
function wc_subscriber_auto_redirect( $boolean ) {
    if( is_subscriber_user() )
        $prevent_access = true;
    return $boolean;
}

// Allow 'subscriber' User Role to  view the Dashboard
add_filter( 'woocommerce_prevent_admin_access', 'wc_subscriber_admin_access', 20, 1 );
function wc_subscriber_admin_access( $prevent_access ) {
    if( is_subscriber_user() )
        $prevent_access = false;

    return $prevent_access;
}

// Show admin bar for 'subscriber' User Role
add_filter( 'show_admin_bar', 'wc_subscriber_show_admin_bar', 20, 1 );
function wc_subscriber_show_admin_bar( $show ) {
    if ( is_subscriber_user() )
        $show = true;
    return $show;
}

代码放在活动子主题(或活动主题)的function.php文件中。

已测试并可用。

如果您希望“订阅者”用户被重定向到仪表板而不是编辑个人资料页面,则只需将get_edit_profile_url()函数替换为get_dashboard_url()函数...


1
你可以使用WooCommerce钩子来重定向不同角色的用户,参见文档:https://docs.woocommerce.com/document/introduction-to-hooks-actions-and-filters/ 我只是在谷歌上搜索了“woocommerce redirect subscribers”,你的答案就出现在第一个结果中 :)
所以您可以使用Woocommerce钩子过滤器 woocommerce_login_redirect 解决网站问题,根据用户角色重定向到所需页面。
function wc_custom_user_redirect( $redirect, $user ) {
// Get the first of all the roles assigned to the user
$role = $user->roles[0];
$dashboard = admin_url();
$myaccount = get_permalink( wc_get_page_id( 'myaccount' ) );
if( $role == 'administrator' ) {
    //Redirect administrators to the dashboard
    $redirect = $dashboard;
} elseif ( $role == 'shop-manager' ) {
    //Redirect shop managers to the dashboard
    $redirect = $dashboard;
} elseif ( $role == 'editor' ) {
    //Redirect editors to the dashboard
    $redirect = $dashboard;
} elseif ( $role == 'author' ) {
    //Redirect authors to the dashboard
    $redirect = $dashboard;
} elseif ( $role == 'customer' || $role == 'subscriber' ) {
    //Redirect customers and subscribers to the "My Account" page
    $redirect = $myaccount;
} else {
    //Redirect any other role to the previous visited page or, if not available, to the home
    $redirect = wp_get_referer() ? wp_get_referer() : home_url();
}
return $redirect;
}

add_filter( 'woocommerce_login_redirect', 'wc_custom_user_redirect', 10, 2 );

请查看源代码:https://gist.github.com/lmartins/28186383883d7c5ec644


3
谢谢。我也找到了这个,但它不能正常工作。我已经逐步检查了代码,发现钩子从未被调用,因为它不是 WooCommerce 登录,而是 WordPress 登录! - LauraTheExplorer

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接