简体   繁体   中英

Redirect specific user role to specific location after Wordpress login

I need to redirect the Shop manager user to the orders page i added this in functions.php but not worked

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

   }

}

Use the following instead:

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;
}

Code goes in function.php file of your active child theme (or active theme). It should works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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