Recently, I have been struggling to get this right. Creating a separate database tables for Multisite.
Multisite has option for “Network Activate”. When enabling the plugin, it should create a separate database table for each of the site. At the same time we also need to consider that it should be deleted when we “uninstall” it. There are also scenarios like creation/deletion of new site under multisite.
I followed various notes on Google, but didn’t work for me then I noticed a plugin “Activity Log” that had this functionality, so getting inspired I re-created a plugin that will just create tables upon activation.
Let me know if you find it useful.
To create a plugin, we need to create a folder inside “Plugins” directory.. lets name it “Custom Test”
Inside it, there should be a file custom-test.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<?php /* Plugin Name: Custom Test Plugin URI: https://www.newzealandgoonline.co.nz Description: This plugin is for testing creating db in multisite Version: 1.0 Author: NZGoOnline Author URI: https://www.newzealandgoonline.co.nz/ License: GPL2 License URI: https://www.gnu.org/licenses/gpl-2.0.html Text Domain: custom-text */ defined( 'ABSPATH' ) or die( 'No script kiddies please!' ); define( 'CUSTOM_TEST__FILE__', __FILE__ ); define( 'CUSTOM_TEST_BASE', plugin_basename( CUSTOM_TEST__FILE__ ) ); include( plugin_dir_path( __FILE__ ) . '/custom-table.php'); ?> |
Now add second file, let’s name it ‘ custom-table.php ‘ in the same folder.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
<?php if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly class nzgo_Maintenance { public static function activate( $network_wide ) { global $wpdb; if ( function_exists( 'is_multisite') && is_multisite() && $network_wide ) { $old_blog_id = $wpdb->blogid; $blog_ids = $wpdb->get_col( "SELECT blog_id FROM {$wpdb->blogs}" ); foreach ( $blog_ids as $blog_id ) { switch_to_blog( $blog_id ); self::_create_tables(); } switch_to_blog( $old_blog_id ); } else { self::_create_tables(); } } public static function uninstall( $network_deactivating ) { global $wpdb; if ( function_exists( 'is_multisite') && is_multisite() && $network_deactivating ) { $old_blog_id = $wpdb->blogid; $blog_ids = $wpdb->get_col( "SELECT blog_id FROM {$wpdb->blogs};" ); foreach ( $blog_ids as $blog_id ) { switch_to_blog( $blog_id ); self::_remove_tables(); } switch_to_blog( $old_blog_id ); } else { self::_remove_tables(); } } public static function nzgo_new_blog_installer( $blog_id, $user_id, $domain, $path, $site_id, $meta ) { global $wpdb; if ( is_plugin_active_for_network( __FILE__ ) ) { $old_blog_id = $wpdb->blogid; switch_to_blog( $blog_id ); self::_create_tables(); switch_to_blog( $old_blog_id ); } } public static function nzgo_delete_blog( $blog_id, $drop ) { global $wpdb; $old_blog_id = $wpdb->blogid; switch_to_blog( $blog_id ); self::_remove_tables(); switch_to_blog( $old_blog_id ); } protected static function _create_tables() { global $wpdb; $sql = "CREATE TABLE IF NOT EXISTS `{$wpdb->prefix}nzgo_email` ( `histid` int(11) NOT NULL AUTO_INCREMENT, `user_caps` varchar(70) NOT NULL DEFAULT 'guest', `action` varchar(255) NOT NULL, `object_type` varchar(255) NOT NULL, `object_subtype` varchar(255) NOT NULL DEFAULT '', `object_name` varchar(255) NOT NULL, `object_id` int(11) NOT NULL DEFAULT '0', `user_id` int(11) NOT NULL DEFAULT '0', `hist_ip` varchar(55) NOT NULL DEFAULT '127.0.0.1', `hist_time` int(11) NOT NULL DEFAULT '0', PRIMARY KEY (`histid`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;"; require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); dbDelta( $sql ); add_option( 'nzgo_db_version', "1.0" ); } protected static function _remove_tables() { global $wpdb; $wpdb->query( "DROP TABLE IF EXISTS `{$wpdb->prefix}nzgo_email`" ); delete_option('nzgo_db_version'); } } register_activation_hook( CUSTOM_TEST_BASE, array( 'nzgo_Maintenance', 'activate' ) ); register_uninstall_hook( CUSTOM_TEST_BASE, array( 'nzgo_Maintenance', 'uninstall' ) ); // MU installer for new blog. add_action( 'wpnzgo_new_blog', array( 'nzgo_Maintenance', 'nzgo_new_blog_installer' ), 10, 6 ); // MU Uninstall for delete blog. add_action( 'delete_blog', array( 'nzgo_Maintenance', 'nzgo_delete_blog' ), 10, 2 ); ?> |
And Voila!
Happy Coding 🙂