Upgrade to Pro — share decks privately, control downloads, hide ads and more …

The Wonders of WordPress Multisite

The Wonders of WordPress Multisite

Presented at WordCamp Winnipeg 2013

Avatar for Patrick Johanneson

Patrick Johanneson

June 01, 2013
Tweet

Other Decks in Technology

Transcript

  1. Patrick Johanneson bona fides ➢ Web Coordinator at Brandon University

    ➢ Assisted with BU's first webserver, 1996(ish) ➢ Working with WordPress since v. 1.5 or so ➢ Doing dev work in WordPress Multisite since v. 3.0
  2. What is Multisite? • Multisite allows multiple virtual sites to

    be run from a single WP installation (Codex Glossary) • Prior to version 3.0, WordPress MultiUser was required for this functionality • With WordPress version 3.0, Multisite functionality was rolled into WP's core
  3. Why does BU use Multisite? • BU has several faculties

    with many departments 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 and maintain
  4. 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. • Don't panic!
  5. Users in Multisite • Each site in a Multisite installation

    can have its own unique users, but all users are added to a site-wide "pool" of users • A user who is an Administrator on Site A can be a Contributor on Site B, a Subscriber on sites C and D, and have no privileges at all on Site E • Also, Multisite adds a new administrative level: Super Admin, who can make network-wide changes
  6. Connecting Sites • News and Events – Centralized or distributed?

    • Parents and grandparents – Breadcrumbs – Contact information
  7. News (Centralized) • BU's news lives in the /news/ site,

    and is maintained by a handful of people • The news articles are categorized by site -- eg, news for the English homepage is in the English category, news for Physics in the Physics category, etc. • Custom code fetches & displays the news for each site in the BU network
  8. 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 site switch_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 );
  9. 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>'; } // endif switch_to_blog( $original_site ); if( strlen( $article_list ) > 0 ) { echo( $article_list ); } } // end of function
  10. Events (Distributed (well, soon)) • BU is moving to tri.be's

    The Event Calendar, which will allow site editors to maintain their own events • Events may be suggested for promotion by site editors, so that they might appear on the BU Home Page Event listing • Promoted events must be authorized by a site administrator
  11. events.php Assumptions The code that follows is the core of

    the event promotion system. Several checks have been trimmed for brevity: • Checking to ensure we're not in the root site (if we are, 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 isn't already a promoted event for the given $id
  12. 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_site' site.\nPlease do not edit this post; just publish it if appropriate, or ignore it.", 'post_title' => $event_title, 'EventStartDate' => $event_start, 'EventEndDate' => $event_end, ); $new_event_id = tribe_create_event( $args );
  13. // 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 that's done, add the _bu_promoted_event meta to the original $event // using the new event's $post->ID so it can be removed later, if necessary add_post_meta($id, '_bu_promoted_event', $new_event_id, true); // All done }
  14. Site Parents & Breadcrumbs • WordPress Multisite does not support

    nested sub-sites (ie, /arts cannot contain /arts/english) • /arts and /english are two separate sites • With a bit of plugin magic, though, we can set /arts as the parent site of /english, so that breadcrumbs look like this:
  15. Site Parent • Arts is interposed in the breadcrumb via

    an in-house plugin named Site Parent. Site Parent sets an option in the current site with the ID of the selected "Parent".
  16. More Site Parent uses • The Site Parent option is

    also used by the in-house Contact Info plugin • If the current site has no contact information set, the plugin will look to the site's Site Parent, using switch_to_blog() • If there's no contact info there, the plugin ascends level after level, until it gets to the root site • If the root site's contact info is empty, the plugin displays a default string
  17. 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