Created
October 3, 2014 10:54
-
-
Save nielsvr/51894229d2dec3e2be88 to your computer and use it in GitHub Desktop.
Running dbDelta on a different database then WordPress
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Using a different database with dbDelta | |
*/ | |
global $wpdb; | |
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); | |
$previous_wpdb = $wpdb; | |
$wpdb = new wpdb('username','password','database','localhost'); | |
$charset_collate = ''; | |
if ( ! empty( $wpdb->charset ) ) { | |
$charset_collate = "DEFAULT CHARACTER SET {$wpdb->charset}"; | |
} | |
if ( ! empty( $wpdb->collate ) ) { | |
$charset_collate .= " COLLATE {$wpdb->collate}"; | |
} | |
$sql = "CREATE TABLE test_table ( | |
ID mediumint(9) NOT NULL AUTO_INCREMENT, | |
PRIMARY KEY ID (id) | |
) ".$charset_collate.";"; | |
dbDelta( $sql ); | |
$wpdb = $previous_wpdb; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Good idea - helped me. Thanks.