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. 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
  2. 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
  3. 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
  4. 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
  5. Plugin Actions ▪ Activate ▪ Deactivate ▪ Uninstall ▪ Network

    Activate ▪ Activate ▪ Network Deactivate ▪ Deactivate ▪ Uninstall
  6. What is needed for a plugin or theme to be

    compatible with WordPress multi-site ?
  7.  Database  Autoloaded Options  Plugins  Theme 

    Rewrite Rules  $GLOBALS       Understanding Context
  8.  Database  Autoloaded Options  Plugins  Theme 

    Rewrite Rules  $GLOBALS             Understanding Context
  9.  Database  Autoloaded Options  Plugins  Theme 

    Rewrite Rules  $GLOBALS             Understanding Context
  10.  Database  Autoloaded Options  Plugins  Theme 

    Rewrite Rules  $GLOBALS             Understanding Context
  11.  Database  Autoloaded Options  Plugins  Theme 

    Rewrite Rules  $GLOBALS                /  / Understanding Context
  12. 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()
  13. Network Plugins <?php /** * Plugin Name: Plugin only intended

    for network activation. * Network: true */
  14. Network Plugins <?php function prefix_network_activate( $network_wide ) { if (

    true === $network_wide ) { // Do network activation actions. } } register_activation_hook( __FILE__, 'prefix_network_activate' );
  15. Non-Network Plugins <?php 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' );
  16. 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 ); } } }
  17. 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 ) } }
  18. Dealing with User Data  Differentiate between global and local

    data  "Blog" specific meta keys  is_super_admin() greyerbaby