Moderator
Yui
(@fierevere)
永子
Migration plugins usually handle export and replacing domain in the database very well.
i.e. Duplicator
It is not recommended to do direct replace in the database dump or with SQL query,
because some settings are stored in the serialized arrays and they will break.
You can also use this 3rd party PHP script: https://github.com/interconnectit/Search-Replace-DB/releases
thank you will check the php script
I use the free WP-SYNC-DB plugin, which can do a find and replace so you can change your old URL into the new one. It can do big databases and the images too with an optional media-library extension. It can push (from local to remote) or pull (from remote) and it saves your settings so it becomes an easy process.
https://wp-sync-db.github.io
-
This reply was modified 6 months, 1 week ago by
eagerbob.
What @eagerbob said. Hasn’t let us down for years. Direct link to repo: https://github.com/wp-sync-db
Hi, the drawback of using a search and replace plugin for the database is that you can get as a result an inconsistent or corrupted database. For that reason always do a backup first. Many plugins serialize their data in a different way, thus a simple search and replace will not always work.
The best solution is to add an entry to the hosts file before you start developing your WordPress website in local. On Linux the path is /etc/hosts
. On windows, C:\Windows\system32\drivers\etc\hosts
. You have to edit the file as an administrator. For example, add this entry:
127.0.0.1 mydomainame.com
Doing this you don’t need to do any kind of search and replace in the future.
Another trick is to replace all strings localhost with your domain just before the output is returned to the user. This is the trick:
add_action( 'muplugins_loaded', 'wp_replace_domain_buffer_start' );
add_action( 'shutdown', 'wp_replace_domain_buffer_end' );
// functions
function wp_replace_domain_buffer_start { ob_start( 'wp_replace_domain_buffer_replace' ); }
function wp_replace_domain_buffer_end { @ob_end_flush(); }
function wp_replace_domain_buffer_replace( $buffer ) {
return str_replace( array( '127.0.0.1:8000', 'mydomain.com' ), $buffer );
}
I am choosing the hook muplugins_loaded
because it’s the first hook to execute WordPress.