Redirecting from login

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

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x