If your site is fairly small, you could...
- Log into phpMyAdmin.
- Export your database as an SQL file.
- Make a copy of your database SQL file so you have a backup of your original database, in-case you screw up.
- Open up your database SQL file in a text editor.
- Find and replace
domain1.com
with domain2.com
.
- Save your database SQL file.
- Log back into phpMyAdmin.
- Import your database SQL file.
EDIT 03/18/2016
AFTER you've done the above steps, please proceed with the following procedure:
Step 1 – Create a function to use.
function mbe_migrate_widgets() {
}
Step 2 – Specify what to search for and what to replace with.
function mbe_migrate_widgets() {
$old_domain = 'http://dev.';
$new_domain = 'http://beta.';
}
Step 3 – Retrieve all WordPress widgets.
function mbe_migrate_widgets() {
$old_domain = 'http://dev.';
$new_domain = 'http://beta.';
global $wpdb;
// Get all Widgets
$widgets = $wpdb->get_results(
"
SELECT `option_id`, `option_name`
FROM $wpdb->options
WHERE `option_name`
LIKE '%widget_%'
"
);
}
Step 4 – Loop through all WordPress widgets, and retrieve the information for each widget.
function mbe_migrate_widgets() {
$old_domain = 'http://dev.';
$new_domain = 'http://beta.';
global $wpdb;
// Get all Widgets
$widgets = $wpdb->get_results(
"
SELECT `option_id`, `option_name`
FROM $wpdb->options
WHERE `option_name`
LIKE '%widget_%'
"
);
if ( ! $widgets ) {
return false;
}
$actions = array();
foreach ( $widgets as $widget ) {
// Retrieve the Widget data.
$widget_data = get_option( $widget->option_name );
}
return $actions;
}
Step 5 – Perform the search and replace on the widget data and update the database records.
function mbe_migrate_widgets() {
$old_domain = 'http://dev.';
$new_domain = 'http://beta.';
global $wpdb;
// Get all Widgets
$widgets = $wpdb->get_results(
"
SELECT `option_id`, `option_name`
FROM $wpdb->options
WHERE `option_name`
LIKE '%widget_%'
"
);
if ( ! $widgets ) {
return false;
}
$actions = array();
foreach ( $widgets as $widget ) {
// Retrieve the Widget data.
$widget_data = get_option( $widget->option_name );
// Update the Widget data changing old domain to new domain.
$update = update_option(
$widget->option_name,
recursive_array_replace( $old_domain, $new_domain, $widget_data )
);
if ( $update ) {
$actions['success'][] = $widget->option_id;
} else {
$actions['failed'][] = $widget->option_id;
}
}
return $actions;
}
Step 6 – Include custom recursive array replacement function.
// Recursive String Replace - recursive_array_replace(mixed, mixed, array);
function recursive_array_replace( $find, $replace, $array ) {
if ( ! is_array( $array ) ) {
return str_replace( $find, $replace, $array );
}
$newArray = array();
foreach ( $array as $key => $value ) {
$newArray[ $key ] = recursive_array_replace( $find, $replace, $value );
}
return $newArray;
}
There you have it. Run that function once and all of your widgets should magically start working again.
Note: It's up to you where you place this code or how you execute it. Please also replace the variables $old_domain
and $new_domain
accordingly.
Extra Note: Please also keep in mind, this example only effects widgets. If you have other parts of your website which contain URLs in serialized data, please modify your database query accordingly. ( I'm talking about this portion of the code: SELECT option_id, option_name FROM $wpdb->options WHERE option_name LIKE '%widget_%'
)
I would post a link to my blog about this topic, but don't wish to appear "spammy". This is essentially a copy/paste from an article I wrote on my website as of 03/18/2016. The contents in this answer might become outdated and the content in the article on my website may become more relevant in the future. Please, if you're interested, check my profile for a link to my website.
Useful explanation of serialized data: Anatomy of a serialize()'ed value.