Voglio che il mio plugin sia installato su ogni blog e crei tabelle di database per blog. Ho questo codice:
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 {
}
Il codice è ispirato a questo post del blog [http://shibashake.com/wordpress-theme/write-a-plugin-for-wordpress-multi-site]
Il file SQL è costituito da:
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)
);
Tuttavia, non funziona, il plug-in crea tabelle proprio come in un normale wordpress ma non su ogni blog nell'ambiente multisito.
Per favore aiuto!