Multisite Admin Login Redirect

Hi guys,

I found the code below, that redirects multsite admin user logins when they reach “domain.com/wp-admin/”, instead of “domain.com/user/wp-admin/”, I still see “You attempted to access..”, etc. Can you help please? Is it possible conflict with other plugins?

The code:

<?php
function ds_login_redirect( $redirect_to, $request_redirect_to, $user )
{
if ($user->ID != 0) {
$user_info = get_userdata($user->ID);
if ($user_info->primary_blog) {
$primary_url = get_blogaddress_by_id($user_info->primary_blog) . 'wp-admin/';
if ($primary_url) {
wp_redirect($primary_url);
die();
}
}
}
return $redirect_to;
}
add_filter('login_redirect','ds_login_redirect', 100, 3);
?>

Best,

Jordan

  • Philip John
    • DEV MAN’s Apprentice

    Hiya!

    I’m a bit confused by your initial description of your issue;

    that redirects multsite admin user logins when they reach “domain.com/wp-admin/”, instead of “domain.com/user/wp-admin/”, I still see “You attempted to access..”, etc

    Can you just clarify that please? Perhaps provide screenshots if that will help to demonstrate the issue you’re having.

    Thanks,

    Phil

  • jdkint
    • New Recruit

    I put an home button "ADMIN" at my front end template(see screenshot), so that logged in multisite users can click and go back to their admin panel. I used the following code in my template functions.php to add this button:

    function new_nav_menu_items($items) {
    $homelink = '<li class=&quot;home&quot;><a href=&quot;' . home_url( '/' ) . '&quot;>' . __('Home') . '</a></li>';
    $adminlink = '<li class=&quot;home&quot;><a href=&quot;' . get_admin_url( $blog->userblog_id ) . '&quot;>' . __('Admin') . '</a></li>';
    $items = $homelink . $items;
    $items = $items . $adminlink;
    return $items;
    }
    add_filter( 'wp_nav_menu_items', 'new_nav_menu_items' );

    But the ADMIN button link made from the function "get_admin_url( $blog->userblog_id )" is domain.com/wp-admin/ instead of domain.com/BLOG/wp-admn/

    So when logged in multsite user clicks on this ADMIN button, he is redirected to the page and sees the message in the attached screenshot.

    I wanted to use the code in my last post to automatically redirect the users to their admin area, instead of seeing this message and clicking the "Visit Dasboard" manually, but seems not working.

    Thanks,

    Jordan

  • Philip John
    • DEV MAN’s Apprentice

    Thanks for the clarification.

    That’s not something I’m aware of how to do I’m afraid.

    What you’d need to do is loop through all the sites that the user is a user of and see which one they have site admin access to, grab it’s ID and filter that in.

    That’s custom development beyond the scope of the support we can really provide here though I’m afraid.

    Thanks,

    Phil

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.