I currently have two login redirects in my general-functions.php file in my mu-plugins directory for my multisite installation.

The first one is for someone who logs in from a specific page. That person should get redirected back to that page after logging in. A "redirect_to" parameter in the URL handles this when the login URL is as follows: http://example.com/wp-login.php?redirect_to=/example-page/

The second one is for everyone else logging into the admin section. Those folks should get redirected to: /wp-admin/my-sites.php

Here are the two functions:

function my_login_redirect( $redirect, $user ) {
    $redirect = isset( $_GET['redirect_to'] ) ? $_GET['redirect_to'] : wp_get_raw_referer();
    return $redirect;
add_filter( 'user_registration_login_redirect', 'my_login_redirect', 9, 2 );

function admin_default_page() {
    return '/wp-admin/my-sites.php';
add_filter('login_redirect', 'admin_default_page', 10);

Both functions work on their own. But sitting in the same functions file, the second one wins out and prevents the first one from working. I have tried to combine them, but unsuccessfully. Obviously, it would make sense to check for a "redirect_to" parameter and perform that redirect if it exists, then redirect everyone else to the My Sites page.

But for whatever reason, I cannot figure out how to do this. I've tried changing the priority of the add_filter, as you can see, but that didn't work. I have tried changing the order in which the functions appear within the functions file. That didn't work. I have tried combining the guts of each function within an if/else statement in either function, and that didn't work for me.

They are referencing two different hooks, so that may be my problem, but I need a way to either combine the two functions or make them function properly independent of each other without the My Sites function winning out every time, which is the current result.

Any help appreciated. Thanks!

  • I originally had an answer for you. However, in looking at your question/code a little more closely, you've got something odd. What is "user_registration_login_redirect"? That's not a WP hook (at least as far as I know of). Is this part of a plugin? – butlerblog May 2 at 20:30
  • You know what, I did find that function in the support forum for a plugin (not a plugin I have installed). But it worked for my site. I couldn't find that hook in the WP codex, but for some reason it worked. – jaboi May 2 at 20:56
  • I removed the second function which redirects to My Sites, and I tried the first function changing only the hook to login_redirect instead of user_registration_login_redirect. When I did that, logging in refreshes the WP login screen in a "logged in" state -- it does not redirect to /example-page/ like the original function. (It does technically log you in, however.) – jaboi May 2 at 21:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.