Skip to content

Instantly share code, notes, and snippets.

@hartliddell
Created February 9, 2015 23:28
Show Gist options
  • Save hartliddell/edd7e0ddf37a02a5470b to your computer and use it in GitHub Desktop.
Save hartliddell/edd7e0ddf37a02a5470b to your computer and use it in GitHub Desktop.
Wordpress Migrate Script
<?php
/**
* Function migrateV2 updates Database tables values: site_ul, home, domain, post url
*
* @param string $urlToReplace migrate from http://xsite.com to htttp://ysite.com
*/
function migrateV2($urlToReplace){
global $table_prefix;
$error = false;
$local_url = $urlToReplace;
//Connetc to DB using PHP connection.. Make sure the DB_HOST, DB_USER, DB_PASSWORD are set up correctly...
$g_link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD) or die('Could not connect to mysql server.');
mysql_select_db(DB_NAME, $g_link) or die('Could not select database.');
//Check if Domain is different from DOMAIN_CURRENT_SITE
$site_query = "SELECT * FROM " . $table_prefix . "site where domain = '" . DOMAIN_CURRENT_SITE . "'";
$site_check = mysql_query($site_query);
$site = mysql_fetch_row($site_check);
if(!is_array($site)){
$values = [ array( table => 'options', column => 'option_value', value => WP_HOME, where => 'option_name ="home"'),
array( table => 'options', column => 'option_value', value => WP_SITEURL, where => 'option_name="siteurl"'),
array( table => 'blogs', column => 'domain', value => DOMAIN_CURRENT_SITE),
array( table => 'site', column => 'domain', value => DOMAIN_CURRENT_SITE),
array( table => 'sitemeta', column => 'meta_value', value => WP_SITEURL, where => 'meta_key="siteurl"'),
array( table => 'posts', column => 'guid', value => WP_SITEURL, replace => TRUE ),
array( table => 'posts', column => 'post_content', value => WP_SITEURL, replace => TRUE ),
];
$error = updateTables($table_prefix, $values, $path='', $local_url);
// Now with all the sub blogs content tables.
$query = "SELECT * FROM " . $table_prefix . "blogs";
$tables_query = mysql_query($query);
$values = [ array( table => 'options', column => 'option_value', value => WP_HOME, where => 'option_name ="home"'),
array( table => 'options', column => 'option_value', value => WP_SITEURL, where => 'option_name="siteurl"'),
array( table => 'posts', column => 'guid', value => WP_SITEURL, replace => TRUE ),
array( table => 'posts', column => 'post_content', value => WP_SITEURL, replace => TRUE ),
];
while ($row = mysql_fetch_array($tables_query)) {
$n = $row['blog_id'];
if ($n != SITE_ID_CURRENT_SITE) {
$path = substr($row['path'], 0, -1);
$multi_table_prefix = $table_prefix.$n.'_';
$error = updateTables($multi_table_prefix, $values, $path, $local_url);
}
}
$dataerror = '<font color="#FF0000">Something went wrong migrating "' . $local_url . '" to "' . WP_SITEURL . '"</font>';
$datasuccess = '<font color="#0033cc">Database Migrated from "' . $local_url . '" to "' . WP_SITEURL . '"</font>';
echo $error ? $dataerror : $datasuccess;
}
mysql_close($g_link);
}
/**
*
* @param type $prefix use wordpress $table_prefix
* @param type $values associative array of query values (table, column, content)
*/
function updateTables($prefix, $values, $path, $urlToReplace){
$err = FALSE;
$path = empty($path) ? '' : $path;
foreach ($values as $item) {
if(isset($item[where])){
$query = 'UPDATE ' . $prefix . $item[table] .' SET '.$item[column].' ="'.$item[value]. $path.'" where '.$item[where];
}elseif(isset($item[replace])){
$query = 'UPDATE ' . $prefix . $item[table] .' SET '.$item[column].' = REPLACE ('.$item[column].', "'.$urlToReplace.'", "'.$item[value].'")';
}else{
$query = 'UPDATE ' . $prefix . $item[table] .' SET '.$item[column].' ="'.$item[value]. $path . '"';
}
if(mysql_query($query)){
echo $query .' - OK'."<br>";
}else{
echo $query .' - <font color="#ff0000">Failed</font>'."<br>";
$err = true;
break;
}
}
return $err;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment