Making my plugin multi-site compatible

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]

Read More

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!

Related posts

Leave a Reply

1 comment