brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: wp search-replace --dry-run shows what a WordPress domain change would touch

WordPress
~/tips/tip-wp-cli-search-replace-dry-run

wp search-replace 'http://old.example' 'https://www.example.nl' --dry-run

Moving a WordPress site to a new domain means changing the old address everywhere in the database, and a wrong replace is painful to undo. WP-CLI lets you see the damage before it happens.

wp search-replace 'http://old.example' 'https://www.example.nl' --dry-run

It prints a table with the number of replacements per database table and changes nothing. If the numbers look sensible (posts, postmeta, options), run the same command without --dry-run. If they look strange, you just saved yourself a restore.

WordPress stores a lot of data as serialized PHP, where every string has its length written next to it. A plain SQL replace that changes the text length breaks that data, and settings or widgets silently stop working. wp search-replace understands serialized data and fixes the lengths.

  • --skip-columns=guid leaves post GUIDs alone, which is the usual advice, because a GUID is an identifier that only looks like a link.
  • --all-tables includes tables that do not start with your prefix.
  • --precise uses slower but more exact handling of serialized data when the default misses something.
  • --report-changed-only shortens the table to rows that actually change.

WP-CLI is a command-line tool for WordPress, and many hosts have it installed. Run wp --info to check, and run the commands from the folder that holds wp-config.php, or point at it with --path=/var/www/html. On a managed host you often log in over SSH first. Without SSH access, a plugin such as Better Search Replace does the same job with a similar dry-run option, though I trust the command line version more because the output is easier to read.

Take a backup with wp db export backup.sql, and if you can, try the replace on a copy of the site first. Watch out for the difference between http:// and https:// and with or without www: replace the exact string you want, otherwise you end up with half-converted links. Afterwards, flush caches and check the site.

Further reading: WP-CLI: wp search-replace, On this site: wp media regenerate –only-missing fixes images after a theme change, On this site: wp core verify-checksums tells you if someone touched your WordPress files

More WordPress tips wp user application-password create anna backup-script --porcelain define( 'MEDIA_TRASH', true ); wp cli check-update wp theme delete --all wp media image-size All WordPress tips (85) All topics

Link