繁体   English   中英

Wordpress登录后将特定用户角色重定向到特定位置

[英]Redirect specific user role to specific location after Wordpress login

我需要将商店经理用户重定向到我在functions.php中添加的订单页面。php 但没有用

add_action('init', 'edit_for_shop_manager');

function edit_for_shop_manager(){

    $user = wp_get_current_user();

   if(wc_user_has_role($user,'shop_manager')){

       function admin_default_page() {
           return home_url().'/wp-admin/edit.php?post_type=shop_order';
       }
       
       add_filter('login_redirect', 'admin_default_page');

   }

}

请改用以下内容:

add_filter( 'login_redirect', 'login_redirect_shop_manager_on_orders_list', 10, 3 );
function login_redirect_shop_manager_on_orders_list( $redirect_to, $request, $user ) {
    $defined_user_role = 'shop_manager'; // The defined user role

    if( isset($user->roles) && is_array($user->roles) && in_array( $defined_user_role, $user->roles ) ) {
        $redirect_to = admin_url('edit.php?post_type=shop_order'); // Custom redirection url

        wp_safe_redirect( $redirect_to ); // Force redirection
        exit(); // Mandatory to avoid errors
    }
    return $redirect_to;
}

代码进入您的活动子主题(或活动主题)的 function.php 文件。 它应该有效。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM