Pro Yearly is on sale from $80 to $50! »

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!
---------------------------------------------------------------

2776198ea9584b6c0d4b494293b8d635?s=128

Juliette Reinders Folmer

October 15, 2016
Tweet

Transcript

  1. The Secret to Mastering WordPress Multi-site

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

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

  6. How Does It Work ? xhowardlee

  7. 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
  8. 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
  9. 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
  10. 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
  11. Plugin Actions ▪ Activate ▪ Deactivate ▪ Uninstall ▪ Network

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

    compatible with WordPress multi-site ?
  13. NOTHING (for most plugins and nearly all themes)

  14. Fine Tuning for the Other 20% djouby

  15.  Database  Autoloaded Options  Plugins  Theme 

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

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

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

    Rewrite Rules  $GLOBALS             Understanding Context
  19. Switching Between Blogs switch_to_blog() restore_current_blog() ms_is_switched()

  20.  Database  Autoloaded Options  Plugins  Theme 

    Rewrite Rules  $GLOBALS                /  / Understanding Context
  21. Uh oh....

  22. 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()
  23. Confused yet ?

  24. “ People will write code

  25. Become a Better Developer

  26. Network Plugins <?php /** * Plugin Name: Plugin only intended

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

    true === $network_wide ) { // Do network activation actions. } } register_activation_hook( __FILE__, 'prefix_network_activate' );
  28. 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' );
  29. 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 ); } } }
  30. 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 ) } }
  31. Deactivation & Uninstall Routines jackiebabe

  32. Dealing with User Data  Differentiate between global and local

    data  "Blog" specific meta keys  is_super_admin() greyerbaby
  33. Rewrite Rules Jeff Djevdet flush_rewrite_rules()

  34. Scheduling Cron Events  Differentiate between global and local data

     is_main_site() irkengirdib
  35. is_multisite()

  36. 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
  37. Thanks! Any questions ? Slides: https://speakerdeck.com/jrf @jrf_nl @jrfnl @jrf