Login Redirect
-
Hi all,
I am trying to incorporate a very lightly styled (via the Custom Login Page Customizer plugin) into my site, and I’m having a problem with user redirects after login.
Specifically, I need users logging in from a specific page to return to that page following login. I need users logging in from the home url to return to the home url. No one but admin should be directed to /wp-admin/.
I’ve inserted the following code into the functions.php file of my child theme:
function my_login_redirect( $redirect_to, $request, $user ) { //is there a user to check? if ( isset( $user->roles ) && is_array( $user->roles ) ) { //check for admins if ( in_array( 'administrator', $user->roles ) ) { // redirect them to the default place return $redirect_to; } else { return home_url(); } } else { return $redirect_to; } } add_filter( 'login_redirect', 'my_login_redirect', 10, 3 );
This does the trick, for the most part. Except that when a user logs in from a page other than the homepage–say, from the comments section of a post–she’s now kicked back to the home url, rather than to the page or post she was trying to reach. I would like her to be redirected to the referring url.
Requisite disclaimers: I’m new to this. Not a practised coder. Know next to zero PHP.
Site: https://knowtheory.org
Theme: Zerif Lite
- The topic ‘Login Redirect’ is closed to new replies.