• Resolved kmuth

    (@kmuth)


    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

Viewing 6 replies - 1 through 6 (of 6 total)
  • Moderator bcworkz

    (@bcworkz)

    For non-admin users, instead of return home_url(); , try return $_SERVER['HTTP_REFERER'];

    Because another filter could be influencing the redirect location, you want your filter to have the final say in the outcome. To help accomplish this, change the priority parameter in the add_filter() call (currently 10) to a large number, perhaps 999.

    Thread Starter kmuth

    (@kmuth)

    Thanks for this, bcworkz, but I’ve tried inserting $_SERVER['HTTP_REFERER'] in place of home_url(), which just loops the user back to the login page.

    Moderator bcworkz

    (@bcworkz)

    Oops, sorry about that ?? I ignored my own advice in my own testing, I didn’t do the 999 thing and had another plugin interfering when I tested if $_SERVER['HTTP_REFERER'] would work.

    Unless you want users to end up at a specific place or the default, the ‘login_redirect’ filter is not going to help because there’s no way for PHP to know where the user really came from. Usually the default should work fine. Have you tried simply removing the filter and let the default work? Just comment out the add_filter() line to see how that might work.

    Thread Starter kmuth

    (@kmuth)

    Ah, yes… the default works great, if someone’s logging in from, say, a comment section. But when they login from the menu, it sends them to the Dashboard. I want to keep them on the front end.

    Moderator bcworkz

    (@bcworkz)

    Argh! Of course. I’ve some serious lapses in reasoning happening here ??
    You could rearrange your callback’s logic to intercept any redirects to the admin area, except for admins of course.

    function my_login_redirect( $redirect_to, $request, $user ) {
    	//is there a user to check?
    	if ( isset( $user->roles ) && is_array( $user->roles ) && in_array( 'administrator', $user->roles )) {
    		// redirect admins to the default place
    		return $redirect_to;
    	}
    	if ( false != strpos( $redirect_to, '/wp-admin')) {
    		return home_url();
    	}
    	return $redirect_to;
    }
    add_filter( 'login_redirect', 'my_login_redirect', 10, 3 );

    The only way to improve upon this would be to alter the login links themselves to include a ‘redirect_to’ URL parameter containing the URL of the current page. For example:
    wp_loginout($_SERVER['REQUEST_URI']);
    might output something like hxxp://example.com/wp-login.php?redirect_to=hxxp://example.com/my-blog-entry/

    With http instead of hxxp of course. How this is accomplished on your site depends on how the login links are generated in the first place.

    Thread Starter kmuth

    (@kmuth)

    Cheers! Thanks for the help bcworkz.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Login Redirect’ is closed to new replies.