WordPress Multisite


Published on

A talk from WordCamp Winnipeg 2013.

Published in: Technology, Business
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

WordPress Multisite

  1. 1. The Wonders of WordPressMultisite
  2. 2. Patrick Johannesonbona fides➢ Web Coordinator at Brandon University➢ Assisted with BUs first webserver, 1996(ish)➢ Working with WordPress since v. 1.5 or so➢ Doing dev work in WordPress Multisite since v.3.0
  3. 3. What is Multisite?● Multisite allows multiple virtual sites to be runfrom a single WP installation (Codex Glossary)● Prior to version 3.0, WordPress MultiUser wasrequired for this functionality● With WordPress version 3.0, Multisitefunctionality was rolled into WPs core
  4. 4. Why does BU use Multisite?● BU has several faculties with manydepartments within them● Each faculty and department has a website● Each website has its own coterie of editors● With WP Multisite, this is easy to set up andmaintain
  5. 5. How does one set up Multisite?● See the WordPress Codex "Create A Network"page for a full explanation● You need to edit your wp-config.php file...● ...set up your Network in /wp-admin/...● ...and then edit your wp-config.php and.htaccess files again.● Dont panic!
  6. 6. wp-config.phpdefine( WP_ALLOW_MULTISITE, true );/* Thats all, stop editing! Happy blogging. */
  7. 7. Tools > Network Setup
  8. 8. Network Setup
  9. 9. Network Setup complete
  10. 10. wp-config.phpdefine( WP_ALLOW_MULTISITE, true );define( MULTISITE, true );define( SUBDOMAIN_INSTALL, false );$base = /;define( DOMAIN_CURRENT_SITE, example );define( PATH_CURRENT_SITE, / );define( SITE_ID_CURRENT_SITE, 1 );define( BLOG_ID_CURRENT_SITE, 1 );/* Thats all, stop editing! Happy blogging. */
  11. 11. Users in Multisite● Each site in a Multisite installation can have itsown unique users, but all users are added to asite-wide "pool" of users● A user who is an Administrator on Site A canbe a Contributor on Site B, a Subscriber onsites C and D, and have no privileges at all onSite E● Also, Multisite adds a new administrative level:Super Admin, who can make network-widechanges
  12. 12. Network Users list
  13. 13. Add New User -- "classic" vs. Multisite
  14. 14. Adding Sites● The Super Admin(s) can add sites
  15. 15. Connecting Sites● News and Events – Centralized or distributed?● Parents and grandparents– Breadcrumbs– Contact information
  16. 16. News (Centralized)● BUs news lives in the /news/ site, and ismaintained by a handful of people● The news articles are categorized by site --eg, news for the English homepage is in theEnglish category, news for Physics in thePhysics category, etc.● Custom code fetches & displays the news foreach site in the BU network
  17. 17. Music Homepage
  18. 18. add_action( bu_news_region, bu_get_news );function bu_get_news() {// Current site$site_name = get_bloginfo( name );if( News == $site_name ) return;// no point doing anything *in* the News site$cat_id = bu_get_category_from_site( $site_name );$url = get_bloginfo( wpurl );// Some site details$news_site = get_blog_details( News );$original_site = get_current_blog_id();// so we can come back later// switch to the news siteswitch_to_blog( $news_details->blog_id );// get the posts with category matching the site name$args = array( category => $cat_id, numberposts => 4 );$articles = get_posts( $args );
  19. 19. if( is_array( $articles ) && ! empty( $articles ) ) {$article_list = "<h2>$site_name News</h2>";$article_list .= <dl>;foreach ( $articles as $post ) {setup_postdata( $post );$article_list .= <dt><a href=" . get_permalink() . "> . get_the_title() . </a></dt><dd><span class="news-date"> . get_the_date() . </span></dd>;} // end foreach$article_list .= </dl>;} // endifswitch_to_blog( $original_site );if( strlen( $article_list ) > 0 ) {echo( $article_list );}} // end of function
  20. 20. Events (Distributed (well, soon))● BU is moving to tri.bes The Event Calendar,which will allow site editors to maintain theirown events● Events may be suggested for promotion bysite editors, so that they might appear on theBU Home Page Event listing● Promoted events must be authorized by a siteadministrator
  21. 21. events.php AssumptionsThe code that follows is the core of the event promotionsystem. Several checks have been trimmed for brevity:● Checking to ensure were not in the root site (if weare, bail out with a return; )● The following must all be true:– tribe_is_event()– event is in the appropriate category in the sub-site– there isnt already a promoted event for the given $id
  22. 22. add_action( save_post, bu_event_promote );function bu_event_promote( $id ){if( ! isset($id) || ! is_numeric($id) ) { return(false); }$from_site = get_bloginfo(name);$from_site_id = get_current_blog_id();// setup the required variables BEFORE we switch to the home site$event_title = get_the_title( $id );$event_url = get_permalink( $id );$event_start = tribe_get_start_date( $id, false, Y-M-d );$event_end = tribe_get_end_date( $id, false, Y-M-d );switch_to_blog( BLOG_ID_CURRENT_SITE );$args = array(post_status => draft,post_content => "This is a promoted post, from the $from_sitesite.nPlease do not edit this post; just publish it if appropriate, or ignoreit.",post_title => $event_title,EventStartDate => $event_start,EventEndDate => $event_end,);$new_event_id = tribe_create_event( $args );
  23. 23. // Meta info (redirection)$meta = array(_pprredirect_active => 1,_pprredirect_type => 301,_pprredirect_rewritelink => 1,_pprredirect_url => $event_url,);$meta_unique = true;foreach($meta as $key => $value){add_post_meta($new_event_id, $key, $value, $meta_unique);}switch_to_blog( $from_site_id );// back from whence you came// once all thats done, add the _bu_promoted_event meta to the original $event// using the new events $post->ID so it can be removed later, if necessaryadd_post_meta($id, _bu_promoted_event, $new_event_id, true);// All done}
  24. 24. Site Parents & Breadcrumbs● WordPress Multisite does not support nestedsub-sites (ie, /arts cannot contain /arts/english)● /arts and /english are two separate sites● With a bit of plugin magic, though, we canset /arts as the parent site of /english, so thatbreadcrumbs look like this:
  25. 25. Site Parent● Arts is interposed in the breadcrumb via an in-house plugin named Site Parent. Site Parentsets an option in the current site with the ID ofthe selected "Parent".
  26. 26. More Site Parent uses● The Site Parent option is also used by the in-houseContact Info plugin● If the current site has no contact information set, theplugin will look to the sites Site Parent, usingswitch_to_blog()● If theres no contact info there, the plugin ascendslevel after level, until it gets to the root site● If the root sites contact info is empty, the plugindisplays a default string
  27. 27. References● Codex.WordPress.org– Glossary– Before You Create a Network– Create a Network– Function Reference (x1000)– Plugin API pages (filters, hooks, etc)● WordPress.org/support● Tri.be support pages & forums● WordPress.StackExchange.com
  28. 28. Youve got questions,Ive got answers....hopefully.
  29. 29. fin.