Skip to content

Instantly share code, notes, and snippets.

@nielsvr
Created October 3, 2014 10:54
Show Gist options
  • Save nielsvr/51894229d2dec3e2be88 to your computer and use it in GitHub Desktop.
Save nielsvr/51894229d2dec3e2be88 to your computer and use it in GitHub Desktop.
Running dbDelta on a different database then WordPress
<?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;
@GermanPearls
Copy link

Good idea - helped me. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment