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

The Secret to Mastering WordPress Multi-site | PHP[World]

The Secret to Mastering WordPress Multi-site | PHP[World]

Presented on November 17 2016 at PHP[World], Washington DC, United States.
https://world.phparch.com/
---------------------------------------------------------------
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 few have mastered. But it doesn’t have to be hard – it’s all about understanding the difference in behavior 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

November 17, 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. User Capabilities ▪update_core ▪install_plugins ▪upload_plugins ▪update_plugins ▪edit_plugins ▪delete_plugins ▪create_users ▪edit_users

    ▪delete_users ▪edit_files ▪install_themes ▪upload_themes ▪update_themes ▪edit_themes ▪delete_themes ▪unfiltered_html Super admin: Site admin:  
  6. Plugin Actions ▪ Activate ▪ Deactivate ▪ Uninstall ▪ Network

    Activate ▪ Activate ▪ Network Deactivate ▪ Deactivate ▪ Uninstall
  7. Cron 1 set of cron hooks and hooked-in commands 1

    set of cron hooks and hooked-in commands per site
  8. What is needed for a plugin or theme to be

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

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

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

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

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

    data  "Blog" specific meta keys  is_super_admin() greyerbaby
  18. Determining URLs Network network_home_url() network_site_url() network_admin_url() Current site home_url() site_url()

    admin_url() Another site get_home_url( $blog_id ) network_site_url( $blog_id ) network_admin_url($blog_id )
  19. Working With Options Network add_site_option(..) update_site_option(..) get_site_option(..) delete_site_option(..) Current site

    add_option(..) update_option(..) get_option(..) delete_option(..) register_setting() Another site add_blog_option( $blog_id, ..) update_blog_option( $blog_id, ..) get_blog_option( $blog_id, ..) delete_blog_option( $blog_id, ..)