Skip to content

Multisite

Ladislav Marsik edited this page Apr 23, 2017 · 8 revisions

Manual

https://codex.wordpress.org/Before_You_Create_A_Network and https://codex.wordpress.org/Create_A_Network

Below is an excerpt of how it is done, so you don't read the whole articles :)

  • Multisite is a network of sites under 1 WP installation. Sites are virtual: they do not have special folders. But each site has its own DB (wp_1_, wp_2_, ... tables), and media upload directory.
  • Plugins/themes are installed globally, and can be activated locally per site.

How to (what was done)

  • Prerequisite for localhost setup: Wiki Using 4water.org virtual host on localhost (Linux)
  • Deactivate all plugins
  • Add define( 'WP_ALLOW_MULTISITE', true ); to wp-config.php, above line /* That's all, stop editing! Happy blogging. */
  • After refresh, in wp-admin a new setting will be shown under Tools->Network Setup
  • There are 2 choices for multisite: either subdomains (prague.4water.org, dance.4water.org), or subdirectories (4water.org/prague, 4water.org/dance). For us the 2nd is applicable (we may later use subdomains but probably only as redirects to a more structured subdirectory system. Plus, subdomain structure also needs some server + DNS configuration that would be a time overhead and conflicting with www subdomain)
  • FYI! When setting up on production, weird thing came up (and unluckily was applicable for us): You cannot choose Sub-directory Install if your existing WordPress installation has been set up for more than a month, due to issues with existing permalinks. This problem will be fixed in a future version. ... But we didn't see it fixed ... that is why a hack needed to go in practice on production - see https://codex.wordpress.org/Multisite_Network_Administration#Switching_network_types . Looks like it works, as the warning is there only because of possible conflicts with pages, and at the time of setting up multisite, we did not have any. Multisite also changes the way that uploads are stored, and may break links to your current media (pictures). But this also was not a blocking thing. If it concerns you, you may wanna have a quick look what pictures you now have in Media, so you know what will need to be re-uploaded. Orphaned links are easy to get rid of in wp-admin later. With that in mind, let's continue.
  • Following task is just to go through what is said under Tools->Network Setup: Other define directives need to be added to wp-config.php and .htaccess file needs to be created with specific content. In DB: Suddendly new tables arise: wp_blogs wp_blog_versions wp_signups wp_site wp_sitemeta wp_registration_log, where only the first is of an importance as it is a summary of all sites you have.
  • Once setup is done, My Sites option appears in top menu in WP Admin. You can create any number of Sites through it! After creating a new site, wp_X_ tables are created (where X is an id of the new site). It's good to play with creating sites a little later when all is ready, but up to you ;)

multisite.png

Note: There is a plugin (that we won't use, just for info) WPMU Domain Mapping that can help us achieve that each site can use its own domain, if we're up to it one day (dance4water.org / climbing4water.org) it can be a good idea for better propagation of individual activities.

  • Simple enabling Multisite, was not enough. It could only handle 1 level, e.g. /dance or /language. I needed to create 2 level hierarchy - for this I had to use plugin WP Multi Network https://wordpress.org/plugins/wp-multi-network/ - works great, with little issues (some links in admin don't work + quite problematic is that it is supported only until WP 4.4.4 - we have 4.4.3.. there is no version update in 0.5 year, BUT luckily I saw that some devs are working on the plugin still - see https://github.com/stuttter/wp-multi-network ... it's a risk but there literally is no other option now. We will pray that they continue support, or we can dev it for them :D)

  • Enable WP Multi Network plugin. Also in wp-config.php, line containting DOMAIN_CURRENT_SITE needs to be commented out (per their installation instructions). Now, NETWORKS panel is enabled on the left, if you go to My Sites -> Network Admin. It's a Management of Networks of Multisites ;)

  • Even the WP Multi Network plugin was not enough to achieve 2 level architecture in URLs like /prague/salsa ... its main purpose is to have networks and sites, with 2 level wp-admins and management, but does not guarantee you anything about URLs .. in the end it looks like subdomain + subfolder (prague.4water.org/salsa) would work natively, but /prague/salsa not. SO I found this: https://paulund.co.uk/wordpress-multisite-nested-paths and followed it to hack it :) sunrise.php is in this PR.

Note: If you run into PHP warnings with sunrise.php, try adding this line in the beginning of the if block: $current_site = new stdClass;

  • While trying to debug errors that finally were resolved by sunrise.php, I ran into some useful tips, from https://github.com/stuttter/wp-multi-network (some define directives for cookies, and other subtle changes). I added them to wp-config.php. Also, the article about sunrise.php requires changes in .htaccess. See my files in comments to see all changes! :)
  • SO: Now we can have Multi - Multisite. Exactly what we wanted :) Vocab is: 4water.org/glasgow, /prague etc = NETWORKS, 4water.org/glasgow/dance /glasgow/language = SITES under glasgow network. To make vocab more confusing, also 4water.org/glasgow is considered a site, bc. each network has to has its 'main site' ;)
  • Content: Uploads are now stored in wp-content/uploads in the folder structure sites/<id of the site>/<year>/<month>. That means your previous uploads just broke ;) Don't worry, now it should be safe to remove the broken links and the orphaned files from your system too... Simply using live2loc.py should get you all uploads from the production, Yay!! :) Also once multisite is up, the manual work of activating all plugins on each sub-site, enable Parallax One theme, and Customizer, should be next. BUT again, live2loc.py should get you all settings for all sites!! So if we took care of production we're fine. The only problem remains, that having <# of sites>-times duplicated all logos on server is a waste :( Either having default pics for all our stuff hardcoded, is a solution, or, somehow hacking the media in wp-admin to allow common pics :)
  • Final working examples of wp-config.php and .htaccess are in comments"

wp-config.php (local) example

... lot of settings that are not changed ...

define('WP_DEBUG', false);

/* Multisite */
define('WP_ALLOW_MULTISITE', true );
define('MULTISITE', true);
define('SUBDOMAIN_INSTALL', false);
define('DOMAIN_CURRENT_SITE', $_SERVER['HTTP_HOST']); // suggested per https://github.com/wp-multi-network
define('PATH_CURRENT_SITE', '/');
//define('SITE_ID_CURRENT_SITE', 1); per https://github.com/wp-multi-network not needed anymore
//define('BLOG_ID_CURRENT_SITE', 1); per https://github.com/wp-multi-network not needed anymore
define( 'NOBLOGREDIRECT', '/404/' ); // suggested per https://github.com/wp-multi-network
define( 'WP_HOME', 'http://' . $_SERVER['HTTP_HOST'] ); // suggested per https://github.com/wp-multi-network
define( 'WP_SITEURL', 'http://' . $_SERVER['HTTP_HOST'] ); // suggested per https://github.com/wp-multi-network

// Cookies, suggested per https://github.com/wp-multi-network
define( 'COOKIEHASH', md5( '4water.org' ) );
define( 'COOKIE_DOMAIN', '4water.org' );
define( 'ADMIN_COOKIE_PATH', '/' );
define( 'COOKIEPATH', '/' );
define( 'SITECOOKIEPATH', '/' );
define( 'TEST_COOKIE', 'thing_test_cookie' );
define( 'AUTH_COOKIE', 'thing_' . COOKIEHASH );
define( 'USER_COOKIE', 'thing_user_' . COOKIEHASH );
define( 'PASS_COOKIE', 'thing_pass_' . COOKIEHASH );
define( 'SECURE_AUTH_COOKIE', 'thing_sec_' . COOKIEHASH );
define( 'LOGGED_IN_COOKIE', 'thing_logged_in' . COOKIEHASH );

// Activate sunrise script, per https://paulund.co.uk/wordpress-multisite-nested-paths
define('SUNRISE', TRUE);

/* That's all, stop editing! Happy blogging. */

... rest of the file ...

.htaccess (local) example

RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]

# add a trailing slash to /wp-admin
RewriteRule ^([_0-9a-zA-Z-]+/)?wp-admin$ $1wp-admin/ [R=301,L]

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
# changed per https://paulund.co.uk/wordpress-multisite-nested-paths
RewriteRule  ^(.+)?/(wp-.*) /$2 [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(.*\.php)$ $2 [L]
RewriteRule . index.php [L]

Tips for wp-admin and creating sites

Lastly, some useful tips, for creating sites, and how to navigate in wp-admin now ;)

Tips for wp-admin

  • TIP1: there is new wp-admin/network URL - it looks almost the same as normal wp-admin, but don't be fooled, it's different ... 4water.org/wp-admin is to be considered an admin for a root 4Water site, whereas 4water.org/wp-admin/network is SuperAdmin for all networks and sites! In UI you can get to Network admin when unrolling My Sites top menu item, it is listed first. Once in Network Admin, you can see you have other left menu items than you were used to!! Particularly, there is the NETWORKS menu item, to manage networks. Also, the Plugins / Appearance over here have some intriguing 'Network Activate'/'Network Deactivate' action buttons for plugins, and 'Network Enable / Disable' for themes. As expected, you can use this to enable Plugin / Theme globally.

multisite2.png

  • TIP2: as a superadmin, you can access all wp-admins of all sites! So, prague/wp-admin and prague/dance/wp-admin are all different admins. Watch out, in Chrome when I first put prague/dance/wp-admin it redirects me to root wp-admin.. but after a second try, or navigation via UI (My Sites -> select Site -> Dashboard), it works :) For illustration, below is a screenshot with all our sites now. Rememeber, also the parent site e.g. /prague is considered a site (one that we probably won't use at all, called 4Water Prague), as well as root site / 4Water, that we'll use for 4Water page as opposed to Activity page)

multisite3.png

  • TIP3: to understand 'where you are', look always what is written on the top, next to the house icon :)

multisite.png

  • TIP4: sometimes the links in admin screw up a littlebit, e.g. in NETWORKS -> Clicking 'Dashboard' under the desired Network will get to weird praguewp-admin URL .. :( I got used to it, just using the My Sites -> roll down site -> Dashboard, or, putting url directly into the browser :)

Creating Networks, and sites in network

  • CREATE /prague NETWORK
  • Then go to My Sites -> Network Admin -> Sites and create /language SITE (careful, don't do prague/language as a site URL - it won't work.. )
  • Hierarchy will be achieved by this hack: go to wp_X_options where X is the newly created site id, fix URLs there (putting '/prague/language' instead of just '/language', or correcting slashes if it looks somewhat like '/praguelanguage'). Then go to wp_blogs and do just the same there (here i make it look with the end backslash too: '/prague/language/')). Also, adjust site_id for the blog. It is actually the id of the parent NETWORK, and you can find it in wp_site table. Yes, it's confusing .. in DB the vocab is contrary to our vocab.. wp_site = our networks, wp_blogs = our sites. Anyway, when you have set the parent to the blog (site), DONE, all should work now ;) Really, maybe this step is achievable by admin, but for me it didn't work. So I concluded that sunrise.php 2-level hierarchy hack implies that we don't have a 'standard' install, and we got to do such little hacks now ..
  • At the time of writing we hard-code the google calendar ids being used, in google_calendar_api.php, so please check this file and add the new calendar id here if needed

NOTE: The functionality that we're manually achieving above SHOULD be achievable by going to: NETWORKS -> Edit (e.g. on /prague network), there is this the magic 'Site assignment' section. Hitting -> it should be possible to get 'language' under 'prague', and probably achieve all that we did manually. However like i said, it does not work on our installation. It screws up the sites, in the way that it creates /praguesalsa URLs, or 'unattaches' other sites quite randomly when you play with networks.

  • TIP: if URLs of the sites screw up (e.g. as above playing with -> button and 'unattaching' the sites from their network, which saves weird domain into db such as http://network.zero ...): Similarly as above, the first AID is: go to wp_X_options where X is the screwed-up site, fix URLs there, then go to wp_blogs and fit paths and set the parent site_id there.

Creating standalone site

  • Just go to Sites -> Add new in Network Admin, fill in the information and you have a stand-alone site! (Possibility to make a copy of an existing site if you choose 'Based on site')