1

I have a WP Multisite / Multi Network installation. What I need is to redirect all non-logged and also all logged in users, except logged in network administrators, from the main site example.com (including all nested pages, posts, categories etc.) to the subsite.example.com, that is a segregated multisite network. They share the same database. The network administrator will have access to the main site only after they will log in on the subsite.example.com. In this way, only the logged in network administrators must have access to the main site example.com.

UPDATE

The @seamus-leahy solution works, but only partially. I use his function in a multisite multi-networks installation and all networks in this configuration works like separated WP installations, so the network example.com will not recognize and log in automatically the network administrator when he/she is already logged in on the subsite.example.com (network 2), but logged out from the example.com (network 1). This means that the network administrator, in this context, also cannot access the main network example.com. How to solve this?

4
  • 4
    Can't you just run "!current_user_can('manage_network')" then redirect simply by adding a redirect in your head tag?
    – Christine Cooper
    Commented Oct 14, 2014 at 13:12
  • @christine-cooper, thank you! Will this work: if( !current_user_can( 'manage_network' )) { header( "Location: subsite.example.com",TRUE,301 ); exit(); }? And what solution will be the best/faster?
    – Yuri
    Commented Oct 14, 2014 at 15:12
  • You did accept an answer that only partly works and added a new one to solve the missing part. Please don't do that. Work on the question, work on the answer, update your question with new and additional info occasionally.
    – kaiser
    Commented Oct 16, 2014 at 14:18
  • 1
    Oh, and there's is_super_admin()...
    – kaiser
    Commented Oct 16, 2014 at 14:19

1 Answer 1

3

You'll need to tie into template_redirect hook where you will check the site and the user and then redirect if needed.

function redirect_to_subsite() {
    if( is_main_site() && !current_user_can('manage_network') ) {
        $blog_id = 5; // <<----- Update the ID to the subsite blog ID you want to redirect to
         wp_redirect( get_site_url( $blog_id ) );
         exit();
     }
 }
 add_action( 'template_redirect', 'redirect_to_subsite' );
1
  • @lurie A redirect head tag will be slower. This sends a short HTTP header to redirect instead of a a full HTTP header along with the page content. You are getting "Call to undefined function..." because your redirect code is before the is_main_site() function is included via /wp-blog-header.php file. If you place your code afterwards, you'll still get error as at that point the page has already been sent to the user. Commented Oct 15, 2014 at 4:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.