HOW TO USE A MAIN SITE WP-ADMIN URL FOR ALL SUB-SITES

Hi there heroes!

When a Blog Admin is logged in, and try to enter our cloudstore.pt/mystore (aka wp-admin), he get's an error message saying he does not have privilegies to access that admin, like on the attached image.

A few months ago, Rupok suggested us to use a function like this one: https://wordpress.org/support/topic/force-redirect-users-to-respective-sub-site?replies=16

Here's the thread of the support ticket:
https://premium.wpmudev.org/forums/topic/how-to-use-a-main-site-wp-admin-url-for-all-sub-sites#post-1053619

Unfortunately, we are having an error/warning when using this code, please check the attached screenshot as well.

Also, when that mu plugin active, the user can't see the main site any more (cloudstore.pt), as he get's automatically redirected to his store.

Could you please give some ideas about the best way to achieve this redirect to the correct blog admin, but keep the access to the main site front end?

Sorry for the confusion, but i'm a little lost here. Thanks again for your help!

  • Nithin

    Hi Urbano,

    Hope you are doing good today. :slight_smile:

    Could you please give some ideas about the best way to achieve this redirect to the correct blog admin, but keep the access to the main site front end?

    Were this code working for you before? Did you check whether any other plugin is causing any conflict, you can perform a conflict test to rule this out.

    I'm not sure whether it would fit to your needs, could you please check whether this plugin might help with redirection: https://wordpress.org/plugins/safe-redirect-manager/

    Please let us know how that goes, so that we could give a closer look to your code. Have a nice day. :slight_smile:

    Kind Regards,
    Nithin

  • Kasia Swiderska

    Hello Urbano,

    Huh. There is few things wrong with that code from wp.org and I wasn't able to make it work on my installation to see if I can replicate this warning you see on your site.
    Does this code work on your site at all but shows that warning?
    Despite that code didn't worked on my site I did get that warning but only in debug.log not on the front end.
    Can you check if debug in your wp-config look like that:

    define('WP_DEBUG', true);
    define( 'WP_DEBUG_LOG', true );
    define( 'WP_DEBUG_DISPLAY', false );

    if not, can you make it like that and then test again?

    kind regards,
    Kasia

  • Kasia Swiderska

    Hello Urbano,

    This was the first time we tested the code, we never had it working before. The warning appear only on sub-sites (blogs). Our main blog didn't have warnings.

    Did you check your wp-config.php file for debug defines? Can you hide them if they are enabled?

    The function is not working properly as well, when a user is already logged in and goes to main-site/wp-admin, he gets redirect to the homepage.

    Wait. So code does work because redirects them to their subsites homepages yes? So in this case that line

    $blog_url = $blog->siteurl;

    should be changed like that

    $blog_url = $blog->siteurl . '/wp-admin';

    so there will be redirect to wp-admin not blog homepage.

    kind regards,
    Kasia

    • Urbano

      Hello again Kasia, thanks for your feedback!

      Can you check if debug in your wp-config look like that:

      define('WP_DEBUG', true);
      define( 'WP_DEBUG_LOG', true );
      define( 'WP_DEBUG_DISPLAY', false );

      I only have the WP_DEBUG and it's false. Should I add the other 2?

      Wait. So code does work because redirects them to their subsites homepages yes?

      No, when a user is already Logged In, and goes to cloudstore.pt/wp-admin (this is our main site), the page refresh and nothing happens.

      FYI: When a user is not logged in it works fine, he visits cloudstore.pt/wp-admin, Login, and get's redirect to his /wp-admin sub-site.

      Should I try the code again with your corrections?

      Thanks again, looking forward to hear from you! :slight_smile:

  • Rupok

    Hi Urbano, thanks for asking.

    I ran the code in my test multisite and I could see the issue. So here goes the updated code. I've modified it from running in front-end to run in back-end. So I used "admin_head" instead of "wp_head" hook. This will stop redirecting your users to their subsite in front-end and now they should be able to access main site on front-end. Moreover, that wp.org user did not initialize the $user object before he tried to set a property. So I've fixed that and here goes the updated code:

    <?php
    function wpmu_redirect() {
    	global $current_user;
    	$user = new stdClass();
    	if(is_user_logged_in()&& ($user->ID = 1)) {
    		wp_get_current_user();
    		if ( !is_user_member_of_blog( $current_user->ID ) ) {
    			$blog = get_active_blog_for_user($current_user->ID);
    			$blog_url = $blog->siteurl;
    			wp_redirect ($blog_url);
    		}
    	}
    }
    add_action('admin_head', 'wpmu_redirect');

    I ran it in my test site and it's working fine without any warning. Please inform if you face any further issue in using this. We will be glad to help further.

    Have a nice day. Cheers!
    Rupok

  • Urbano

    Hi there heroes!

    First of all, thanks again for all the help you've been giving me. I've been able to solve every situations with your insights.

    About this specific situation, please allow me to brief further information:

    We have recently changed the MU from sub-directories to sub-domains. This have affected some behaviours on how users login to websites, and now it's working a little different.

    Problem: The Login at the main site (cloudstore.pt) isn't working, even tho I don't think this is a bug.

    So far, I've been able to figure out what's happening. When a new Blog is created, the new Administrator will be signed for Main-Site and His-Blog.

    This means, if the Admin Logins in the Main-Site, the Logins works, but doesn't redirect him to his his-blog.cloudstore.pt/wp-admin, (I believe) because his also a Main-Site user (cloudstore.pt).
    After login at cloudstore, the page get's refreshed with no message, creating confusion for the user. The admin bar on the top is also not loaded after login.

    I figured this out because I created a new Admin for his-blog.cloudstore.pt, and when this new user Logins at cloudstore.pt (main-site) he gets the message we've been trying to avoid (you have no permissions for this website, please click on your site bellow).

    So, my question would be: Can I have new Blogs/Admins created without being signed up in the main-site to avoid this "error"? Or should I try another work-around?

    Before we changed from sub-directories to sub-domains, when a user logged in at the main-site (cloudstore), it was automatically redirect to cloudstore.pt/his-blog/wp-admin. Now it's happening what I explained above.

    FYI: We are using Gravity Forms + User registration Add-on.
    FYI2: We are also using Domain Mapping, not sure how this will work on other domains.

    We have tried the Code Rupok provided, but it doesn't seem to be working. I have disabled the code while we investigate further.

    Could you guys give some insights on the best approach to solve this?

    Sorry for the long description, thanks again for all your help!

  • Nithin

    Hi Urbano,

    Hope you are doing good today. :slight_smile:

    I tested the code what Rupok had provided in my multisite, and it seems to work fine, admin users are directed to their respective subsite. This seems to be more of a conflict in your system, could you please test this in another multisite environment, and check whether it works fine? If possible, could you please test this with all your plugins disabled, and check whether it makes any difference? Performing this, should easily help us rule out where we are having issues with.

    If you are not comfortable is preforming these steps, you could also setup a staging website, so that we could test this extensively in your system: https://codex.wordpress.org/Moving_WordPress#Moving_WordPress_Multisite
    https://premium.wpmudev.org/blog/set-up-staging-site-cloner/
    https://premium.wpmudev.org/blog/testing-environment-wordpress/

    Please let us know how that goes, so that we could give a closer look. Have a nice day. :slight_smile:

    Kind Regards,
    Nithin

    • Urbano

      Hi there Nithin, thanks for your reply!

      Sorry for the delay, but I've been making all the investigations you suggested, and seems like it solved out pretty well.

      I tested the code what Rupok had provided in my multisite, and it seems to work fine, admin users are directed to their respective subsite.

      Indeed, the code works fine. We are using the Gravity Forms plugin from Sybre and I discovered there are some lines forcing the login that are causing the mal function. We have commented those lines and it seems like it's working good. Need's more testing tho.

      So, my question would be: Can I have new Blogs/Admins created without being signed up in the main-site to avoid this "error"? Or should I try another work-around?

      About this, we found an option in Gravity Forms to prevent users from getting registered in the main site. The issue is solved and was being caused by Gravity Forms official plugin.

      Thanks again for your precious help on this, keep up the good work!

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.