Commit eccb01d8346cb6b280f7c99dda36107469a5d700

Authored by Paulo Graça
0 parents
Exists in master

Initial commit

Showing 1 changed file with 34 additions and 0 deletions   Show diff stats
wp_multisite_redirect.php
... ... @@ -0,0 +1,34 @@
  1 +<?php
  2 +/*
  3 +Plugin Name: Redirect Main Site To User 1 Site
  4 +Description: Redirect 'main-site' to 'main-site/sub-site/' for user 1, based on original work of WPSE
  5 +Version: 0.1
  6 +Author: RCAAP
  7 +Author URI: http://www.rcaap.pt
  8 +License: GPL2
  9 +*/
  10 +add_action('parse_request', 'redirect_to_user1_blog');
  11 +function redirect_to_user1_blog(){
  12 + if ( ! is_multisite() ) {
  13 + exit;
  14 + }
  15 +
  16 + #Sniff requests for a specific slug
  17 + if(is_main_site()){
  18 + # the default website by the user Webmaster
  19 + $user_info = get_userdata(1);
  20 + if ($user_info->primary_blog) {
  21 + $url = get_blogaddress_by_id($user_info->primary_blog);
  22 + } else {
  23 + #The URL to redirect TO
  24 + $url = network_site_url();
  25 + }
  26 +
  27 + #Let WordPress handle the redirect - the second parameter is obviously the status
  28 + if (get_site_url() != network_site_url()) {
  29 + wp_redirect($url, 301);
  30 + }
  31 + #It's important to exit, otherwise wp_redirect won't work properly
  32 + exit;
  33 + }
  34 +}
... ...