Created
June 25, 2012 23:31
-
-
Save kythin/2992089 to your computer and use it in GitHub Desktop.
Fix wordpress serialized variables in custom meta data when running a migration
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 | |
/* | |
* Wordpress postmeta find&replace code | |
* | |
* This file should be run on your source database to echo out update statements to run on your migrated database. | |
* It is to address the issue of when doing a find/replace on an sql file during a wordpress migration, that breaks | |
* any content changed within wordpress serialized meta values, particularly if you have custom post types etc. | |
* | |
* Do your normal find/replace over the exported sql, import it into your new database, then run this code and copy the resulting | |
* update SQL into your new database to fix it. | |
* | |
* See line 80 ish for where to actually define your find/replace. | |
* | |
*/ | |
error_reporting(-1); | |
$dbhost = ''; | |
$dbname = ''; | |
$dbuser = ''; | |
$dbpass = ''; | |
$postmeta_table = 'wp_postmeta'; | |
$dbconn = mysql_connect($dbhost, $dbuser, $dbpass); | |
mysql_select_db($dbname, $dbconn); | |
$result = selectarray('SELECT * FROM wp_postmeta'); | |
if (is_array($result)){ | |
foreach($result as $row){ | |
$meta_id = $row['meta_id']; | |
$meta_value = $row['meta_value']; | |
if (is_serialized($meta_value)){ | |
$new_meta_value = make_new_value($meta_value); | |
if (strlen($new_meta_value) != strlen($meta_value)) { | |
ob_start(); | |
echo($meta_value."<br>".strlen($meta_value)."<br><br>".$new_meta_value."<br>".strlen($new_meta_value)."<br>"); | |
echo("<br><hr><hr><br>"); | |
@$debug .= ob_get_clean(); | |
$sql .= " | |
UPDATE wp_postmeta SET meta_value = '".mysql_real_escape_string($new_meta_value)."' WHERE meta_id = '$meta_id'; | |
"; | |
} | |
} | |
} | |
} | |
echo("<pre>$sql</pre>"); | |
function is_serialized($v){ | |
return @unserialize($v)?true:false; | |
} | |
function make_new_value($oldvalue){ | |
$val_a = unserialize($oldvalue); | |
if (is_array($val_a) && count($val_a)) { | |
foreach($val_a as &$val){ | |
//run your find & replace code here | |
//e.g: | |
$val = str_replace('findme', 'replacemewith', $val); | |
} | |
$new_val_ser = serialize($val_a); | |
return $new_val_ser; | |
} else { | |
return $oldvalue; | |
} | |
} | |
function selectarray($sql) { | |
$result = mysql_query($sql) or die(mysql_error()); | |
if (!$result) { | |
debug(mysql_error()); | |
} | |
$table_result=array(); | |
$r=0; | |
while($row = mysql_fetch_assoc($result)){ | |
$arr_row=array(); | |
$c=0; | |
while ($c < mysql_num_fields($result)) { | |
$col = mysql_fetch_field($result, $c); | |
$arr_row[$col -> name] = $row[$col -> name]; | |
$c++; | |
} | |
$table_result[$r] = $arr_row; | |
$r++; | |
} | |
return $table_result; | |
} | |
?> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment