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

The secret to mastering WordPress Multi-site - WCNL16

The secret to mastering WordPress Multi-site - WCNL16

Presented on October 15 2016 at WordCamp Nederland, Utrecht, The Netherlands.
https://2016.netherlands.wordcamp.org/
---------------------------------------------------------------
WordPress multi-site is a powerful tool which lets you run thousands of websites with just one WordPress installation.

Developing for WordPress multi-site, however, is a fine art which too few have mastered.

But it doesn’t have to be hard – it’s all about understanding the difference in behaviour between a single site WP install and Multi-site and knowing which WordPress functions to use, when and how.

Join in and beat the competition by making your plugins compatible with WordPress Multi-site!
---------------------------------------------------------------

Juliette Reinders Folmer

October 15, 2016
Tweet

More Decks by Juliette Reinders Folmer

Other Decks in Programming

Transcript

  1. The Secret to
    Mastering
    WordPress
    Multi-site

    View full-size slide

  2. Hello!
    Juliette
    Reinders Folmer
    @jrf_nl @jrfnl @jrf

    View full-size slide

  3. UI for multi-network setup:
    https://wordpress.org/plugins/wp-multi-network/

    View full-size slide

  4. How
    Does
    It
    Work ?
    xhowardlee

    View full-size slide

  5. File System
    1 WP installation
    1 plugins folder
    1 themes folder
    1 must-use plugins folder
    1 translations folder
    1 uploads folder
    1 WP installation
    1 plugins folder
    1 themes folder
    1 must-use plugins folder
    1 translations folder
    1 uploads folder
    with #n subfolders in a
    sites subdirectory

    View full-size slide

  6. Database
    1 database
    no network tables
    1 options table
    1 set of user tables
    1 set of content tables
    1 database
    1 set of network tables
    #n sets of options tables
    1 set of user tables
    #n sets of content tables

    View full-size slide

  7. Database
    wp_comments
    wp_commentmeta
    wp_links
    wp_options
    wp_posts
    wp_postmeta
    wp_terms
    wp_termmeta
    wp_term_relationships
    wp_term_taxonomy
    wp_users
    wp_usermeta
    wp_users
    wp_usermeta
    wp_comments
    wp_commentmeta
    wp_links
    wp_options
    wp_posts
    wp_postmeta
    wp_terms
    wp_termmeta
    wp_term_relationships
    wp_term_taxonomy
    wp_2_comments
    wp_2_commentmeta
    wp_2_links
    wp_2_options
    wp_2_posts
    wp_2_postmeta
    wp_2_terms
    wp_2_termmeta
    wp_2_term_relationships
    wp_2_term_taxonomy
    wp_3_comments
    wp_3_commentmeta
    wp_3_links
    wp_3_options
    wp_3_posts
    wp_3_postmeta
    wp_3_terms
    wp_3_termmeta
    wp_3_term_relationships
    wp_3_term_taxonomy
    wp_blogs
    wp_blog_versions
    wp_registration_log
    wp_signups
    wp_site
    wp_sitemeta

    View full-size slide

  8. Users
    1 set of users
    1 set of user meta per user
    1 set of user capabilities
    Highest role: admin
    1 set of users
    1 set of user meta per user
    1 set of user capabilities
    per sub-site
    Highest role: super-admin

    View full-size slide

  9. Plugin Actions
    ▪ Activate
    ▪ Deactivate
    ▪ Uninstall
    ▪ Network Activate
    ▪ Activate
    ▪ Network Deactivate
    ▪ Deactivate
    ▪ Uninstall

    View full-size slide

  10. What is needed for a
    plugin or theme to be
    compatible with
    WordPress multi-site ?

    View full-size slide

  11. NOTHING
    (for most plugins and nearly all themes)

    View full-size slide

  12. Fine
    Tuning
    for the
    Other
    20%
    djouby

    View full-size slide

  13.  Database
     Autoloaded Options
     Plugins
     Theme
     Rewrite Rules
     $GLOBALS






    Understanding Context

    View full-size slide

  14.  Database
     Autoloaded Options
     Plugins
     Theme
     Rewrite Rules
     $GLOBALS












    Understanding Context

    View full-size slide

  15.  Database
     Autoloaded Options
     Plugins
     Theme
     Rewrite Rules
     $GLOBALS












    Understanding Context

    View full-size slide

  16.  Database
     Autoloaded Options
     Plugins
     Theme
     Rewrite Rules
     $GLOBALS












    Understanding Context

    View full-size slide

  17. Switching Between Blogs
    switch_to_blog() restore_current_blog()
    ms_is_switched()

    View full-size slide

  18.  Database
     Autoloaded Options
     Plugins
     Theme
     Rewrite Rules
     $GLOBALS















    /

    /
    Understanding Context

    View full-size slide

  19. Terminology
    Network of
    Networks
    •get_network_by_path()
    Network
    • get_site_by_path()
    • WP_Network
    • $current_site
    • is_main_network()
    • get_current_site()
    • is_network_admin()
    • is_super_admin()
    Site
    • WP_Site
    • $current_blog
    • is_main_site()
    • get_blog_details()
    • get_site()
    • get_sites()
    • is_admin()
    • current_user_can()

    View full-size slide

  20. Confused yet ?

    View full-size slide

  21. “ People
    will write
    code

    View full-size slide

  22. Become
    a Better
    Developer

    View full-size slide

  23. Network Plugins
    /**
    * Plugin Name: Plugin only intended for network activation.
    * Network: true
    */

    View full-size slide

  24. Network Plugins
    function prefix_network_activate( $network_wide ) {
    if ( true === $network_wide ) {
    // Do network activation actions.
    }
    }
    register_activation_hook( __FILE__, 'prefix_network_activate' );

    View full-size slide

  25. Non-Network Plugins
    function prefix_no_network_activate( $network_wide ) {
    if ( false === $network_wide ) {
    // Do network activation actions.
    } else {
    add_action( 'network_admin_notices',
    'prefix_admin_notice_no_network_activate');
    }
    }
    register_activation_hook( __FILE__, 'prefix_no_network_activate' );

    View full-size slide

  26. Update, don't Activate
    class Prefix_My_Class {
    const VERSION = '1.0';
    function __construct() {
    $option = get_option( 'prefix_option' );
    if ( false === $option || ! isset( $option['version'] ) ||
    version_compare( $option['version'], self::VERSION, '<') ) {
    $this->upgrade( $option );
    }
    }
    }

    View full-size slide

  27. Update, don't Activate
    class Prefix_My_Class {
    const VERSION = '1.0';
    protected function upgrade( $option ) {
    // Add DB tables
    // Add action to flush rewrite rules at a later hook
    // Add cron jobs
    // etc
    $option['version'] = self::VERSION;
    update_option( 'prefix_option', $option )
    }
    }

    View full-size slide

  28. Deactivation & Uninstall Routines
    jackiebabe

    View full-size slide

  29. Dealing with
    User Data
     Differentiate between
    global and local data
     "Blog" specific
    meta keys
     is_super_admin()
    greyerbaby

    View full-size slide

  30. Rewrite Rules
    Jeff Djevdet
    flush_rewrite_rules()

    View full-size slide

  31. Scheduling Cron
    Events
     Differentiate between
    global and local data
     is_main_site()
    irkengirdib

    View full-size slide

  32. is_multisite()

    View full-size slide

  33. is_network_admin()
    Action:
    'network_admin_menu'
    wp_is_large_network
    ()
    current_user_can
    _for_blog()
    self_admin_url()
    get_current_blog_id
    ()
    Other Useful Snippets

    View full-size slide

  34. Thanks!
    Any
    questions ?
    Slides: https://speakerdeck.com/jrf
    @jrf_nl @jrfnl @jrf

    View full-size slide