Skip to content
Nicolas Juen edited this page Aug 3, 2018 · 4 revisions

Context

The custom tables into WordPress are handled into the $wpdb global object.

Two types of tables

When deadling with custom tables, there is two approaches :

  • Global multisite table
  • Site by site table, created on activation

Site by site table

To make your custom tables compatible with WordPress (switch to blog for example) you need to do this in the main plugin file :

// Plugin tables
global $wpdb;
$wpdb->tables[]     = 'sample_table';
$wpdb->sample_table = $wpdb->prefix . 'sample_table';

Where sample_table is the real name of your table. This is important because WordPress will search/replace this name in the case of switch_to_blog.

Global mulsite table

To make your custom tables compatible with WordPress on mulsite you need to do :

// Plugin tables
global $wpdb;
$wpdb->ms_global_tables[]     = 'sample_table';
$wpdb->sample_table = $wpdb->prefix . 'sample_table';

Where sample_table is the real name of your table. This will not replace the table name on a switch_to_blog call.

Clone this wiki locally