admin管理员组

文章数量:1317904

I want my site to redirect after login to site_url('/') for all users except 'administrator' I also want the admin_bar showing for certain roles ie event_planner. It is all working except that when and event_planner logs on and then clicks on the admin bar to go into admin it redirects to '/'. The hook being used to redirect is 'admin_init' which is clearly wrong. Is there another hook I could use to initially redirect to / on login but not when pressing the dashboard on the admin bar.

Thanks

I want my site to redirect after login to site_url('/') for all users except 'administrator' I also want the admin_bar showing for certain roles ie event_planner. It is all working except that when and event_planner logs on and then clicks on the admin bar to go into admin it redirects to '/'. The hook being used to redirect is 'admin_init' which is clearly wrong. Is there another hook I could use to initially redirect to / on login but not when pressing the dashboard on the admin bar.

Thanks

Share Improve this question asked Oct 28, 2020 at 18:07 PeterBPeterB 72 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 3

There's a filter called login_redirect.

add_filter( 'login_redirect', 'wpse377295_login_redirect', 10, 3 );
function wpse377295_login_redirect( $url, $request, $user ) {
    if ( is_wp_error( $user ) ) {
        // It's possible that the $user param is a WP_Error. If so, bail out.
        return $url;
    }
    if ( ! user_can( $user, 'update_core' ) {
        // Only admin users can update core, normally.
        $url = site_url( '/' );
    }
    return $url;
}

There's a more complete example in the filter documentation's User Contributed Notes.

References

  • login_redirect
  • user_can()

本文标签: hooksRedirecting from login