Automatic login redirect to primary site & block main site admin – WordPress MultiSite

Automatic login redirect to primary site & block main site adminSo once again, while developing something in WordPress MultiSite for a client, I came across a problem where I had to put Automatic login redirect to primary site & block main site admin in WordPress MultiSite setup. The problem was the login link was given from the main site. So when ever a community site member used that link to login, she got redirected to the main site wp-admin, instead of her own primary blog wp-admin. This was a serious trouble for newbie WP MS users as they were getting lost inside the admin section. Now to get back to their own site admin, then had to click Dashboard > My Sites > Select her site > Her site’s admin. Lots of clicks. After googling a bit I found an excellent solution given here. Extending the solution I came up with a complete wordpress mu plugin which does the job. As we have started our series on WP Plugin Development in OOP, so I have written this plugin using PHP classes. We will also see what & how the plugin does things.

#0: Things to do:

  • When a user logs in from main site to the admin, instead of redirecting her to the main site wp-admin redirect her to her primary blog/site’s wp-admin
  • When a user tries to access the main site wp-admin redirect her to her primary blog’s wp-admin
  • All of above should not be applicable for the Super Admin (assumed with user ID 1)

With all the above things in mind, we shall code our mu-plugin. You can put the php file directly in your /wp-content/mu-plugins/ directory to get the things done. No further actions are required as this is activated by default.

If you wish to know, how the plugin does these things so that you can modify it, keep on reading.

#1: login_redirect hook to redirect after login:

WordPress gives us a powerful action hook login_redirect to do our things after a user logs in. We shall hook into this using API. For that, we have written this method:

    /**
     * Redirect the users on login
     * Users should be redirected to their primary site whenever they try to login from anywhere
     *
     * Should be hooked
     *
     * @param string $redirect_to
     * @param string $requested_redirect_to
     * @param string $user
     * @return string
     */
    public function login_redirect($redirect_to, $requested_redirect_to, $user) {
        if ($user->ID != 0 && $user->ID != 1) {
            $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;
    }

#2: Block the main site admin for  users:

WordPress gives another API admin_menu which is meant to be used for creating administrative menus through plugins. But we used a hackish approach here to redirect to the primary blog wp-admin instead of main site wp-admin. Let us see the method.

    /**
     * Redirect the logged in users to their primary site's admin
     * when they try to access the admin of the main site or other site
     *
     * Comment out line 61 to block only the main site admin
     * @global type $current_user
     * @global type $blog_id
     * @return type
     */
    public function admin_redirect() {
        global $current_user, $blog_id;

        if ($current_user->ID == 0 || $current_user->ID == 1)
            return;

        //the below line blocks access ONLY to the main site wp-admin
        //remove the line to block access to all sites except primary
	if ($blog_id !='1') return;

        $primary_url = get_blogaddress_by_id($current_user->primary_blog) . 'wp-admin/';

        if (strpos($_SERVER['REQUEST_URI'], 'wp-admin') && ( $blog_id != $current_user->primary_blog)) {
            wp_redirect($primary_url);
        }
    }

As you can see, both the methods not do things if the current user ID is 1. It is assumed that the Super Admin has got user ID 1 (which is default). However, if this is not your case, then change it accordingly. Also an interesting this is the highlighted line. Comment that out if you want to block access to all sites, except the primary (no matter how many sites the user has got).

#3: Hook the methods:

Obviously, you should have figured it out by now, how the method redirects to her primary site and blocks main site admin. Now to actually hook them, we write our constructor method.

    /**
     * Constructor
     * Hooks the actions
     */
    public function __construct() {
        add_action('login_redirect', array(&$this, 'login_redirect'), 100, 3);
        add_action('admin_menu', array(&$this, 'admin_redirect'));
    }

Pretty simple I guess. Now we just simply instantiate the class:

$itg_wpms_login_redirect = new itg_wpms_login_redirect();

and we are done! Easy wasn’t it?

#4: Understanding the used APIs:

We have used the following three APIs to write the method.

#4.1: get_userdata:

Returns all the metadata of the passed in user. We use the ID from the variable passed to hook to get the user’s metadata. Basically we needed this to get the ID of her primary_blog.

#4.2: get_blogaddress_by_id:

As the name suggests, we get the address of a multisite blog/site by passing in the ID. We have passed the ID of the primary blog to get the address.

#4.3: wp_redirect:

It simply redirects to the passed in URL which is the primary-site-url/wp-admin/. It obviously corresponds to the admin section for default WordPress setup.

#5: The complete code:

So if we put together the code, the it looks like:

<?php

/**
 * The redirect mu-plugin
 * Redirects user to their primary blog admin once they log in (instead of main site dashboard)
 * Redirects user to their primary blog admin when they try to access wp-admin of some other blog
 */
class itg_wpms_login_redirect {

    /**
     * Constructor
     * Hooks the actions
     */
    public function __construct() {
        add_action('login_redirect', array(&$this, 'login_redirect'), 100, 3);
        add_action('admin_menu', array(&$this, 'admin_redirect'));
    }

    /**
     * Redirect the users on login
     * Users should be redirected to their primary site whenever they try to login from anywhere
     *
     * Should be hooked
     *
     * @param string $redirect_to
     * @param string $requested_redirect_to
     * @param string $user
     * @return string
     */
    public function login_redirect($redirect_to, $requested_redirect_to, $user) {
        if ($user->ID != 0 && $user->ID != 1) {
            $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;
    }

    /**
     * Redirect the logged in users to their primary site's admin
     * when they try to access the admin of the main site or other site
     *
     * Comment out line 61 to block only the main site admin
     * @global type $current_user
     * @global type $blog_id
     * @return type
     */
    public function admin_redirect() {
        global $current_user, $blog_id;

        if ($current_user->ID == 0 || $current_user->ID == 1)
            return;

        //the below line blocks access ONLY to the main site wp-admin
        //remove the line to block access to all sites except primary
	if ($blog_id !='1') return;

        $primary_url = get_blogaddress_by_id($current_user->primary_blog) . 'wp-admin/';

        if (strpos($_SERVER['REQUEST_URI'], 'wp-admin') && ( $blog_id != $current_user->primary_blog)) {
            wp_redirect($primary_url);
        }
    }
}

$itg_wpms_login_redirect = new itg_wpms_login_redirect();

So once again here is the download link of the above plugin which you can put directly inside your /wp-content/mu-plugins/ directory.

Hope you find this useful. Don’t forget to share and comment. And if you know any better method than this or are running into any trouble, then, just say!

2 comments

  1. ⎝⏠_⏠⎠ (@danvao)

    It’s great

    …just one quick question (probably its very simple, but after working all night I just can’t see it)

    Whenever a user opens an account in my site, they get to admin their own site and they also subscribe to another that acts like a forum for support

    So this plugin helps to avoid the error page that they were getting, but it also sends them to the “forum” (I’m guessing its just because of the name of the sites), how can i change that?

    Anyhow thanks very much, this was very very helpful.

Comments are closed.