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) {
                switch_to_blog($blog_id);
                _install2();
            }
            switch_to_blog($old_blog);
            return;
        }   
    } 
    _install2();        
}

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_set_time_limit(0);
    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!

link|improve this question
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... – Brady 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
feedback

1 Answer

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

link|improve this answer
Nice plugin. I think you are the author. I couldn't give upvote cos i used 30 votes today :( – Giri 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
That means you didn't add $wpdb->prefix to their name. – scribu Aug 11 '11 at 11:36
feedback

Your Answer

 
or
required, but never shown

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