Moving WordPress to new URL
-
I need to move a WordPress site to a new URL. I have done so, and have run the script to modify the selected fields, such as wp_options option_name = home or siteurl; wp_posts guid; and wp_posts post_content to change the URL.
However, I have a number of wp_options records that have the old URL in a PHP serialized array. I have written a routine that converts these records, but when I do, the elements seem to go away. As a specific example, there is a text widget that has this reference, but when I change the database, the widget disappears from the site as well as the admin interface.
Anyone have any suggestions?
-
I have written a routine that converts these records– how does the script convert the rows?
As often is the case, I found my error just after posting this.
Although the PHP documentation says that str_replace will work on arrays, it does not seem to. I replace that with a recursive routine that goes through the array elements and applies str_replace to non-array objects, hopefully strings. This does seem to work.
If anyone is interested, I would be glad to share the script.
Although the PHP documentation says that str_replace will work on arrays, it does not seem to– yeah, I’ve experienced the same problem, but I never investigated a better solution
If anyone is interested, I would be glad to share the script.– can you post a pastbin link?
Here are the routines I use to change the content. The fixData is called with the DB field I want to change, the old URL base and then new. It seems to work.
/********************************************************************** * This routine will test the target to see if it is serialized data, and it * it is it will unserialize the data, and if an array, will user fixElement * (recursively) to find and replace the old domain with the new one. It * will then serialize the new data and return it. * * If it is not a serialized string, it will just replace the strings and * return the new string. *********************************************************************/ function fixData ($target, $old_domain, $new_domain) { $ut = unserialize ($target); if ($ut === false) { $ut = str_replace ($old_domain, $new_domain, $target); } else { $ut = fixElement ($ut, $old_domain, $new_domain); $ut = serialize ($ut); } return $ut; } /********************************************************************** * This routine will accept an element that is eith an array or assumed to * be a string, and will replace the old domain with the new one. It will * call itself recusively to handl arrays within arrays. *********************************************************************/ function fixElement ($target, $old_domain, $new_domain) { if (is_array ($target)) { $ut = array (); foreach ($target as $i => $t) { $ut[$i] = fixElement ($t, $old_domain, $new_domain); } } else { $ut = str_replace ($old_domain, $new_domain, $target); } return $ut; }
The topic ‘Moving WordPress to new URL’ is closed to new replies.