Search & replace (WordPress)

Uit De Vliegende Brigade
Versie door Jeroen Strompf (overleg | bijdragen) op 15 feb 2023 om 15:42 (→‎Search & Replace Plugins)
(wijz) ← Oudere versie | Huidige versie (wijz) | Nieuwere versie → (wijz)
Naar navigatie springen Naar zoeken springen

Around migrating, URLs within a WordPress database need to be updated. Situations where this may arise:

  • When cloning a site - The most common use case for me
  • When migrating from development to production environment
  • When migrating from https to http (e.g., when migrating a site, this is often the first stage)
  • Sometimes when migrating from http to https
  • When a URL changes.

From https to http

In February 2023, I came across a use case that demonstrated that search & replace is needed when migrating a site from https to http:

Use cases: Migrate example.fr from an old server to a new server:

  • Instantiate the site on the new server
  • Keep DNS entries unchanged (as I first want to check if everythings works). Hence SSL didn't work on the new server
  • Change DNS entry in file <cd>/etc/hosts on my laptop only, so that from my laptop, I can visit the site at its new location
  • Test the site on the new location: Not possible, as the address immediately jumps from http to https as soon as the site is loading

It was only possible to visit the site on the new location, after search & replace like:

wp search-replace https://example.fr http://example.fr

Quite non-intuitive, as the URL stays the same, but the protocol is changed.

With WP-CLI

In general, I use wp search-replace old-url new-url for this. See Wp search-replace (WP-CLI) for details.

Without WP-CLI?

Now in January 2023, I want to migrate a site from a VPS to a 'regular hosting account' without SSH and hence without WP-CLI. Additionally, this site will be available at something like example.com.transurl.nl.

So I need an alternative to the usual WP-CLI approach:

  • Plugins
  • Scripts

Search & Replace Plugins

I suspect that the wp-admin portion of a site works, even when a URL isn't translated yet. This is important since otherwise plugins couldn't be executed:

Search Replace PHP script

Use a standalone PHP script, as these can be invoked from the URL of the site. E.g.: Search Replace DB PHP script.

SQL

Do it directly in SQL using something link update table_name set blub=replace(blub, old-url, new-url), e.g., through PHPMyAdmin. This might be quite tricky, as the WP-CLI command is quite precise in what it targets and what not.

See also

Sources