Skip to main content

Integrating with MultiSite Global Tables Mode

Build an add-on

Part 9 of 11 in the Build an add-on series. Previous: Conditional placeholders. Next: Custom event pages.

On a multisite network, Events Manager can run in global tables mode, where every site stores its events in one shared set of tables and any site can list events from the others. This part makes the add-on's data consistent across the network in that mode. On a single site, or a network without global tables, it changes nothing.

The problem​

The add-on stores two things, and in global tables mode they end up in different places:

  • The event-to-style rows are in the meta table. The EM_META_TABLE constant already points at the network's shared table in this mode, so these rows are shared with no extra work. This is the reason to use the table constants and never build a table name from $wpdb->prefix.
  • The style list is a WordPress option, and each site has its own options table. Style 1 could be "Formal" on one site and "Outdoor" on another, so [events_list blog="1,2,3" style="1"] would mix unrelated events.

What you will build​

A network-wide style list, managed from the main site only.

Before you start

You need a multisite network with global tables mode enabled in Network Admin then Events Manager, and the add-on network-activated. Test on a development network.

Steps​

  1. Add this line at file level, outside my_em_styles_init(), for example under the add_action( 'events_manager_loaded', ... ) line:

    add_filter( 'em_ms_globals', 'my_em_styles_ms_globals' );

    It has to be registered when the plugin file loads, because Events Manager reads the list on init at priority 1, as early as events_manager_loaded itself.

  2. Add the my_em_styles_ms_globals() function below to the end of the file.

  3. Restrict the admin page to the main site. In my_em_styles_admin_menu() from part 1, add the check at the top, so the function reads as in the second block below.

Part 9: a network-wide option
function my_em_styles_ms_globals( $globals ) {
if ( EM_MS_GLOBAL ) {
$globals[] = 'my_em_styles';
}
return $globals;
}
The final menu function
function my_em_styles_admin_menu(): void {
if ( EM_MS_GLOBAL && ! is_main_site() ) {
return; // 9. In global tables mode, styles are managed on the main site only.
}
add_submenu_page( 'edit.php?post_type=' . EM_POST_TYPE_EVENT, 'Event Styles', 'Styles', 'edit_others_events', 'events-manager-styles', 'my_em_styles_admin_page' );
}

How it works​

Global options. em_ms_globals filters a list of option names that Events Manager keeps in the network's sitemeta table instead of each site's options table. Once my_em_styles is on the list, every get_option( 'my_em_styles' ) on any site returns the network value, and update_option() writes to it. None of the other code in the add-on has to change.

EM_MS_GLOBAL is true only in global tables mode. The check keeps the list per-site on a network that doesn't share its events, where separate lists are the right behavior. To test for multisite without global tables, use is_multisite() && ! EM_MS_GLOBAL.

Who can save. Events Manager only writes a global option when the current user is a super admin. For anyone else the update is ignored without an error. Restricting the page to the main site keeps site administrators from seeing a form that would appear to do nothing, but a main-site editor who is not a super admin is still affected. On a network, consider changing the page's capability to manage_network_options.

Existing styles. Styles created on individual sites before this change are not merged. The network value starts empty, so add your styles again from the main site and re-tick them on events.

Test it​

  1. As a super admin on the main site, go to Events then Styles and add a style.
  2. Switch to another site in the network. There is no Styles item in its Events menu.
  3. Edit an event on that site. The Styles box lists the style you added on the main site.
  4. On the main site, add [events_list blog="1,2" style="1"] to a page, using your own site IDs. Events with that style from both sites are listed.

Next​

Part 10: Custom event pages