the code to generate a new table

  • タグ:
  • タグはありません
function yd_wpmuso_create_table( $opt, $blog_id, $master_blog_id ) {
	global $wpdb;
	$table = preg_replace( '/^ydtable_/', '', $opt );
	$query = "SHOW CREATE TABLE $table";
	$res = $wpdb->get_results( $query, ARRAY_A );
	$sql = $res[0]["Create Table"];
	
	switch_to_blog( $master_blog_id );
	$post = preg_replace( '/^' . $wpdb->prefix . '/i', '', $table );
	
	switch_to_blog( $blog_id );
	$sql = preg_replace( '/' . $table . '/i', $wpdb->prefix . $post, $sql );
	$sql = preg_replace( '/^CREATE TABLE/i', 'CREATE TABLE IF NOT EXISTS', $sql );
	$wpdb->query( $sql );
	restore_current_blog();
}