I want my plugin to be installed on each blog and create database tables per blog. I have this code:

register_activation_hook( __FILE__, 'install1' );
function install1() {
    global $wpdb;

    if (function_exists('is_multisite') && is_multisite()) {
        // check if it is a network activation - if so, run the activation function for each blog id
        if (isset($_GET['networkwide']) && ($_GET['networkwide'] == 1)) {
                    $old_blog = $wpdb->blogid;
            // Get all blog ids
            $blogids = $wpdb->get_col($wpdb->prepare("SELECT blog_id FROM $wpdb->blogs"));
            foreach ($blogids as $blog_id) {

function _install2()
    require_once WP_PLUGIN_DIR . '/pluginfolder/functions/database.php';
    require_once WP_PLUGIN_DIR . '/pluginfolder/functions/general.php';
    $db_error = false;

    $sql_file = WP_PLUGIN_DIR . '/pluginfolder/ossq.sql';
    os_db_connect(DB_HOST, DB_USER, DB_PASSWORD);
    os_db_install(DB_NAME, $sql_file);
    if ($db_error != false) {

        //  echo 'instalation successfull';
    } else {


The code is inspired on this blog post [http://shibashake.com/wordpress-theme/write-a-plugin-for-wordpress-multi-site]

The SQL File consist of:

DROP TABLE IF EXISTS address_book;
CREATE TABLE address_book (
   address_book_id int NOT NULL auto_increment,
   customers_id int NOT NULL,
   entry_gender char(1),
   entry_company varchar(255),
   entry_firstname varchar(255) NOT NULL,
   entry_lastname varchar(255) NOT NULL,
   entry_street_address varchar(255) NOT NULL,
   entry_suburb varchar(255),
   entry_postcode varchar(255) NOT NULL,
   entry_city varchar(255) NOT NULL,
   entry_state varchar(255),
   entry_country_id int DEFAULT '0' NOT NULL,
   entry_zone_id int DEFAULT '0' NOT NULL,
   PRIMARY KEY (address_book_id),
   KEY idx_address_book_customers_id (customers_id)

However, Its not working, the plugin does create tables just like on a regular wordpress but not on each blog on the multisite environment.

Please help!

  • whats in ossq.sql file and why not use $wpdb object – Bainternet Aug 9 '11 at 6:38
  • instead of making a new table for every blog installation wouldnt it make more sense to use just one table but add a column for blog_id? That way if there is just one blog the blog ID is always 1. If multisite then the blog id is set as per the site ID... – Scott Aug 9 '11 at 7:51
  • I wanted to do so, but i don't know how to do that. I can make a column for the blog ids however i don't how to create a function for that. – Ken Aug 11 '11 at 9:00
  • This works great, I have recently added it in my code. newzealandgoonline.co.nz/… – NZGO Apr 16 '18 at 10:08

The Proper Network Activation meta-plugin was written precisely for cases like this.

  • Nice plugin. I think you are the author. I couldn't give upvote cos i used 30 votes today :( – PrivateUser Aug 9 '11 at 11:31
  • The plugin doesn't solve my problem. The two sites where I network activated my plugin still have 1 database tables. What I need is for them to have separate database tables each. – Ken Aug 11 '11 at 8:59
  • 3
    That means you didn't add $wpdb->prefix to their name. – scribu Aug 11 '11 at 11:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.