Viewing 2 replies - 1 through 2 (of 2 total)
  • There are a lot of ways to go about this depending on your situation. I needed to come up with a similar solution. However, I also wanted to redirect users back to the referring page when logging in to WordPress.

    Here’s what I came up with:

    if ((isset($_GET['action']) && $_GET['action'] != 'logout') || (isset($_POST['login_location']) && !empty($_POST['login_location']))) {
    	add_filter('login_redirect', 'login_redirect_back_to_referrer', 10, 3);
    	function login_redirect_back_to_referrer()
    	{
    		$location = $_SERVER['HTTP_REFERER'];
    		wp_safe_redirect($location);
    		exit();
    	}
    }
    
    if (class_exists('user_switching')) {
    	function wp_admin_redirect_user_switching_plugin()
    	{
    		if (current_user_can('list_users')) return;
    		global $pagenow;
    		if ($pagenow == 'users.php') {
    			wp_redirect(admin_url('/'));
    			exit;
    		}
    	}
    	add_action('admin_init', 'wp_admin_redirect_user_switching_plugin');
    }

    The first part I use to redirect users back to the rerferring page when loggin in. I use this function regardless even if the plugin is not active. I think it makes things more user friendly than always redirecting users to the dashboard/profile.

    The second part checks if user-switching is active. If it is I check to see if the user has permissions to view users.php. If they don’t, I go ahead and redirect them to my desired page.

    If you don’t want to redirect to referrer page you will need to make some changes. Basically, just look in the plugin to see how it’s redirecting. If I remember correctly it uses php $_REQUEST to redirect. So you could check the $_REQUEST to dynamically change the location using the login_redirect filter. This is probably the most ideal solution.

    Other options (not tested):

    – Use $_SERVER['REQUEST_URI'] to check URL or parse_url query to check if the url/query matches the url user-switching normally redirects to. If it does, redirect it to your desired location

    – Remove the query argument and then add your own using add_query_arg

    I’m not sure if you are referring to “network” as a wordpress multisite network or just your site. If you mean a wordpress multisite network you can also use the “is_super_admin” and is_network_admin conditional tags

    Anyway, while these are merely suggestions, since I’m not totally sure if I understand what you need, you can see that there are a ton of different ways to do this. I’m guessing the developer has a better solution as well.

    You can add a couple of hooks in your child theme’s functions.php file, to achieve this. See examples below:

    To redirect to a custom page, such as a page only logged in users have access to:

    add_action( 'switch_to_user', 'switch_to_user_redirect' );
    function switch_to_user_redirect()
    {
    	wp_redirect( home_url( 'userpages/mydetails' ) );
    	exit();
    }

    To redirect back to the main wp admin page:

    add_action( 'switch_back_user', 'switch_back_user_redirect' );
    function switch_back_user_redirect()
    {
    	wp_redirect( home_url( 'wp-admin' ) );
    	exit();
    }

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘How to redirect after switching?’ is closed to new replies.