Okay I’ve been trying to get this to work, and I just cannot see why it’s creating one and not the other. So now I’ve tried this:
function install_tables() {
global $wpdb;
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
$sales = $wpdb->prefix . "sales";
$settings = $wpdb->prefix . "settings";
if($wpdb->get_var("show tables like '$sales'") != $sales) {
$sql = "CREATE TABLE " . $sales . " (
// blah blah whole bunch of info here
);
dbDelta($sql);
}
if($wpdb->get_var("show tables like '$settings'") != $settings) {
$sql = CREATE TABLE " . $settings . " (
// blah blah second table info here
); ";
dbDelta($sql);
}
}
register_activation_hook(__FILE__,'install_tables');
I’m still having no table-love. It’s creating the $sales table, but still won’t create the $settings. What is it that I’m missing? I know this *can* be done. In fact, I got the “new trial” from another plugin that uses the exact same format to create multiple tables – but it’s not doing it.
I’ve also tried using $sql for the $sales, and $query for the settings, in case what’s happening is it’s overriding rather than combining. But still no luck. What is it that I’m missing? Any help would be appreciated.
Okay, I finally found a solution.
function install_tables() {
global $wpdb;
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
$num = 0;
$it_tables[$num]['table_name'] = $wpdb->prefix . 'sales';
$it_tables[$num]['table_sql'] = "CREATE TABLE " . $wpdb->prefix . "sales (
//info here
) $charset_collate;";
$num++;
$it_tables[$num]['table_name'] = $wpdb->prefix . 'settings';
$it_tables[$num]['table_sql'] = "CREATE TABLE " . $wpdb->prefix . "settings (
//info here
) $charset_collate;";
foreach($it_tables as $it_table) {
if(!$wpdb->get_var("SHOW TABLES LIKE '{$it_table['table_name']}'")) {
$wpdb->query($it_table['table_sql']);
}
}
}
This, I placed in a separate file called “tableinstall.php”, and in the plugin “index” page, I included this:
include_once(dirname (__FILE__) . '/tableinstall.php');
register_activation_hook(__FILE__,'install_tables');
This seems to be working just fine. Hope it helps someone else.
Hi, I have the same problem, I will try your solution.
What value do you use for $charset_collate???
thanks in advance,
Gilberto
It worked!!! 6 tables and everything is OK!
Thank you very much!