So I thought I would save other people from the same frustration. The reason you can’t simply do a find and replace on the database when moving from one url to another is because the data stored in option_value for option_tree is serialized. So for every value in the array, it has the number of characters associated with it. So the values that I posted before:
s:4:”logo”,s:88:”http://stage.google.com”
… are messed up because it thinks that there are 88 characters in the url and when there are not, it breaks the site.
So Here’s what I did. I initially attempted to go directly to the database to put the serialized string. For some reason it failed. So I tracked down how it was done in the plugin (used the get_option(‘option_tree’)) and started the process of storing it to an array and then finding the values I needed to replace and replacing them and storing them back into the array.
You can simply change the str_replace values with your own to make it work. After that, I attempted to serialize the array and then dump it back into the option but that didn’t work. So I am simply echoing out the serialized array and then base64 encoding it to use it in the import portion of the plugin. If you import it in using the developer’s tool… then it works like a champ. The last thing I had to do was to run a couple of update commands on the database:
UPDATE wp_options SET option_value=replace(option_value, “stage-v2.google.com”, “www.google.com”);
UPDATE wp_posts SET guid=replace(guid, “stage-v2.google.com”, “www.google.com”);
UPDATE wp_options SET option_value=replace(option_value, “www.someotherdevelopmentsite.com/google”, “www.google.com”);
UPDATE wp_posts SET guid=replace(guid, “www.someotherdevelopmentsite.com/google”, “www.google.com”);
Here’s the php code:
<?php
define('WP_USE_THEMES', false);
global $wp, $wp_query, $wp_the_query, $wp_rewrite, $wp_did_header;
require('wp-load.php');
$array = replaceVal(get_option('option_tree'));
function replaceVal(&$array) {
foreach ($array as $key=>$val){
if (is_array($val)) {
$val = replaceVal($val);
}
else {
$val = str_replace('stage-v2.google.com','www.google.com',$val);
$val = str_replace('www.anotherdevelopmentsite.com/google','www.google.com',$val);
}
$array[$key] = $val;
}
return $array;
}
$serializedandencoded = base64_encode(serialize($array));
echo '<pre>';print_r($serializedandencoded);echo '</pre>';
?>
I would really like a way to change the database directly without having to run the import, but it was as good as I could get with needing it really quickly.
I could see the developer possibly adding a “change url” field so that all the pictures etc could be changed dynamically. Or maybe switching to json would alleviate painpoint as well. All just suggestions. If anyone figures out how to write it directly to the database instead of doing an import I would LOVE to see it. 🙂