Last active
August 21, 2020 04:03
-
-
Save trvswgnr/c1da77b6051eeff1e6627f054f911be2 to your computer and use it in GitHub Desktop.
WordPress - Replace Table Prefixes
This file contains hidden or 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 | |
/** | |
* Replace and rename table prefixes | |
* | |
* @param string $old The old table prefix to be replaced. | |
* @param string $new The new table prefix. | |
* @author Travis Aaron Wagner | |
*/ | |
function taw_replace_table_prefixes( $old, $new ) { | |
global $wpdb; | |
$sql_get_tables = "SHOW TABLES LIKE '$old%'"; | |
$bad_tables = $wpdb->get_results( $sql_get_tables ); | |
if ( empty( $bad_tables ) ) { | |
return; | |
} | |
foreach ( $bad_tables as $table ) { | |
foreach ( $table as $key => $name ) { | |
$new_name = str_replace( $old, $new, $name ); | |
$sql = "ALTER TABLE $name RENAME TO $new_name"; | |
try { | |
$wpdb->query( $sql ); | |
} catch ( Exception $e ) { | |
echo 'Caught exception: ' . esc_html( $e->getMessage() ); | |
} | |
} | |
} | |
} | |
add_action( | |
'init', | |
function() { | |
global $wpdb; | |
taw_replace_table_prefixes( 'dcl_', $wpdb->prefix ); | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment