WordPress

SSH into your server and run search-replace from the CLI

A database-wide find and replace is dangerous in phpMyAdmin but trivial with WP-CLI. Here's how to SSH in and swap URLs without breaking serialized data.

Changing a domain, moving from http to https, or migrating a site means updating URLs everywhere in the database. Do it with raw SQL and you’ll corrupt serialized data. wp search-replace does it right. But first you have to get onto the server.

Connect with SSH

SSH gives you a secure shell on the server. You need a username, a server address and often a port. The syntax is:

ssh youraccount@your-server.com -p 22

# with a specific key:
ssh -i ~/.ssh/my-key youraccount@your-server.com -p 22

Once inside, navigate to the WordPress root (see part one on cd and ls) until you see wp-config.php.

Always dry-run first

Rule number one: never run a real search-replace without first seeing what it would change. The --dry-run flag shows the number of matches without touching anything.

wp search-replace 'http://old.com' 'https://new.com' --dry-run

Does the count look reasonable? Drop --dry-run and run it for real.

wp search-replace 'http://old.com' 'https://new.com'

Why WP-CLI and not SQL

WordPress stores a lot of data serialized — widget and plugin settings, for example. Serialized strings contain their own length. A raw SQL replace changes the text but not the length number, and the data breaks. wp search-replace understands serialized data and recalculates the lengths for you.

Handy flags to know

  • --all-tables — include tables without the WordPress prefix (e.g. after a migration).
  • --precise — slower but handles tricky serialized data most safely.
  • --skip-columns=guid — never touch the GUID column; it shouldn’t change.
  • --export=dump.sql — write the result to a file instead of the database.

Take a backup first — always

Before you change the database, export it. One command, and you have a way back:

wp db export backup-before-replace.sql

Dry-run, back up, run. In that order — every time.

— Kepler Cloud

The full WP-CLI series

Rather not do it yourself? At Kepler, WP-CLI and SSH access are part of Managed WordPress — we keep core, themes and plugins updated for you. And you can always SSH into your own cloud server and run the commands above whenever you like.

Bygg på ett moln du faktiskt äger.

Ingen bindningstid. En körande VM innan du betalar en krona.