Migrating WP – Interconnect – WP CLI – Better S&R

More from this category




Migrating>Migrating WordPress Across Hosts, Servers, and URLs

https://code.tutsplus.com/tutorials/migrating-wordpress-across-hosts-servers-and-urls–wp-20104

Last week, we took a look at how to migrate a WordPress installation from a local server to production. Today, we are going to take a beginner’s approach to moving your WordPress blog to a new host/server or even changing its address (URL). 

So if you are changing your provider or just need a change in address (or even both), here is an in-depth video on how to do it. If you’re a developer, this video is going to be very basic, but it’s a great reference for anyone who is trying out their first migration.

WordPress is a complex piece of software and to everyday users without a lot of dev experience, it can seem much more complex. So first off, I’m going to break WordPress down and define some terminology so you don’t get lost. Then I’m going to slowly take you into the actual nuts and bolts of moving a WordPress installation.

There are a lot of ways to define WordPress, but for the purposes of what we’ll be covering in this tutorial, your WordPress installation is comprised of 2 parts:

  1. Files on your website (php, css, html, javascript, etc.)
  2. A Database (holds all of the information)

In order for WordPress to work, it needs the Files to do the work of creating your website while the Database holds the content. These files are mostly made of up the PHP programming language. In order to see the files of WordPress, you have to login to your host or server via FTP (File Transfer Protocal). You can access the FTP of your site by using a program like WinSCP, Firebug or FileZilla for free. The information to connect to it should be provided by your webhost. To move these files, all you need to do is digitally move them like you would a file on a thumb drive. The only difference is that you are using the FTP program to get it to your webhost.

https://wpengine.com/support/find-replace/

WordPress Database Search and Replace

On this page: Running a search and replace on your WordPress database is a crucial task. Learn when and how to use a search and replace, and when one may be done for you.

Running search and replaces is crucial to successfully running a website. Below we’ll cover when and how you can use a search and replace, and when one might be automatically run for you.

Terminology
Search and Replace Plugins
Search and Replace with WP CLI
Automated Search and Replace

Terminology

We’ll use the following terms throughout this article:

Source – this is the original environment that you are copying from

Target – this is the destination of the copy or deploy

The target will be replaced with your source data.


Search and replace plugins

Whether you are looking to correct mixed content, or simply need to update hard-coded references of your domain after taking a site live, running a search and replace is a common practice when managing a website.

The easiest way to perform a search and replace on your own is with a plugin. Some recommended plugin options include:

NOTE: It’s recommended to always test before executing search and replace by using the “dry run” feature.


Search and replace with WP CLI

WP CLI is considerably more flexible than a plugin, as a plugin is simply leveraging parts of WP CLI in the first place, however it comes with its own set of risks. Be sure to first run any command using the --dry-run flag to preview the changes before executing them and always make a checkpoint first.

Review our guide to learn how to connect to SSH Gateway.

The basic structure of a WP CLI search and replace looks like this:

wp search-replace 'source' 'target'

Where source is the phrase you’re searching for, and target is the phrase you’re replacing with. Be sure to leave the single quotes around the phrase to ensure only the correct content is found and updated.

If you want to run a search-replace only on specific tables, you can add them in like this:

wp search-replace 'source' 'target' wp_posts wp_options

NOTE: It is highly advised to always use flags (detailed below) in conjunction with the basic commands.

For more information on how to use a WP CLI search and replace, check out the WordPress developer’s guide.

Search-replace flags

Flags are extremely important to a search and replace to help ensure you’re getting the intended result. Add as many flags, separated by a space, as-needed to achieve the desired result.

wp search replace 'olddomain.com' 'newdomain.com' --precise --skip-columns=user_email --skip-columns=guid --dry-run

--dry-run – Preview the command, but do not execute it. It is always recommended to run the command first with this flag.

--precise – Force the use of PHP (instead of SQL) which is more thorough, but slower. This will also ensure serialized data is search/replaced. While it is slower, it is more effective and recommended to always use this flag.

--network – On a multisite, enable replacement on all subsites in the database. Without this flag, the command is run only on the selected subsite’s tables.

--all-tables – Enable replacement on ALL tables in the database, regardless of the prefix, and even if not registered on $wpdb. Overrides --network.

--skip-columns=user_email – Skips the user_email column which will prevent email addresses from inadvertently being updated.

--skip-columns=guid – Used primarily for feeds. The GUID must never change, even if you shift domains around.

--report-changed-only – Results only display the tables that have changes available. This is helpful when working with large databases or multisites.

--skip-plugins --skip-themes – Prevents plugin or theme code from rendering when WP CLI commands are run. If your site is returning an error when running commands, use this.

--regex – Runs the search using a regular expression (without delimiters). Warning: search-replace will take about 15-20x longer.

Search-Replace Examples

Search for your old domain, and replace with your new domain:

  • Be sure to update the values in bold to your own
  • Uses PHP, preserves emails and GUIDs
  • Will not execute any code, only preview results, to execute live, remove --dry-run

wp search-replace 'olddomain.com' 'newdomain.com' --precise --skip-columns=user_email --skip-columns=guid --dry-run

Search and replace to correct mixed content:

  • Similar to the command above, this command also includes protocol and will change it from http to https to resolve mixed content.
  • Be sure to replace domain.com with your domain in the source and destination.
  • Will not execute any code, only preview result, to execute live, remove --dry-run

wp search-replace 'http://domain.com' 'https://domain.com' --precise --skip-columns=user_email --skip-columns=guid --dry-run


Automated Search and Replace

Moving a WordPress installation between different environments can be a pain. That’s why WP Engine has created tools to help you copy and deploy your WordPress sites between WP Engine installs. When you interact with an environment on WP Engine using the following of our tools, we will automatically handle the domain search and replace process for you:

These search and replaces are performed using WP CLI and are protocol-less (http/https), case-insensitive and include serialized data.

The process also supports URL cases such as new/long TLDs (.solutions), punycode TLDs (test.XN--VERMGENSBERATUNG-PWB), and second level TLDs (test.co.uk).

Lastly, our cases include adjustments to account for the following characters which may come before or after a domain URL:

  • Blank spaces
  • New lines
  • Brackets [ ]
  • Parenthesis ( )
  • Curly Brackets { }
  • Angle brackets < >

NOTE: We will NOT search and replace email addresses.

Automated search and replace process

  1. Determine the domain for both the source and destination environments
  2. Verify Core Checksums
  3. Perform the following domain search and replaces:
    • source.com -> target.com
    • www.source.com -> www.target.com
    • subdomain.source.com -> subdomain.target.com
    • source.com/path -> target.com/path
    • <a href=”source.com”>source.com</a> -> <a href=”target.com”>target.com</a>
    • /wp-content/www.source_.com_.png -> /wp-content/www.source_.com_.png
  4. Purge server caches
  5. Reset site file permissions

NEXT STEP: Learn how to identify and correct mixed content


https://kinsta.com/knowledgebase/wordpress-search-and-replace/

One of the first methods is to use a free plugin called Better Search Replace.

Better search WordPress replace
Better search WordPress replace

Better Search Replace plugin

The Better Search Replace plugin is developed by the awesome WordPress team over at Delicious Brains and is inspired by the interconnect/IT PHP script in option 2 below. These guys know a thing or two about WordPress development. As of writing this, the plugin currently has 300,000+ active installs with a 4.5 rating. It is also actively maintained. You can download it from the WordPress repository or by searching for it within your WordPress dashboard under “Add New” plugins. Some of the features include:

  • Serialization support for all tables
  • The ability to select specific tables
  • The ability to run a “dry run” to see how many fields will be updated
  • No server requirements aside from a running installation of WordPress
  • WordPress Multi-site support

There are hundreds of different use case scenarios. As you can see you in our example below, in the Better Search Replace options, we could easily search for any hard-coded HTTP references and update with the HTTPS version.

better search replace options
better search replace options

Better Search Replace options

There is also a pro version of the plugin available which gives you even more features. You can check out their official documentation on how to better use the plugin.

3. interconnect/it Search Replace DB PHP Script

A second option you have for running a WordPress search and replace is to use a free PHP script from interconnect/it called Search Replace DB.

This script has been around since 2011 and the developers actively update it. Interconnect/it is known for its great WordPress consulting work and development. To use the script, simply download the zip file, extract the folder called search-replace-db-master, and rename it to something secret of your choosing. In our example, we renamed it to update-db-1551. Then upload it via FTP, SFTP, or SCP to your web server’s public directory. This is typically the same directory that contains your /wp-content folder. Then navigate to your secret folder in your browser, such as https://domain.com/update-db-1551.

interconnect search replace script interconnect search replace script

The script will automatically attempt to find and populate the database field but you must check that the details are correct and that it is for the database you wish to carry out a search/replace operation on. You can click on “dry run” first to see what it will be updating/replacing. Then when you are ready click on “live run” which will perform the database updates and the WordPress search and replace.

search replace options search replace options

It is also very important due to security reasons that you delete this script after you are done! You can click the “delete me” button. It you don’t, it could leave your website open to attacks. It is also recommended to double check on your web server and confirm that the folder/script has been completely removed.

4. WP-CLI

And finally, the last recommend method for performing a WordPress search and replace is to do it directly with WP-CLI. This is for you WordPress developers out there. For our Google Cloud customers, we provide SSH access so you can use WP-CLI. If you are unfamiliar with WP-CLI you can check out our in-depth post on managing WordPress from the terminal.

Here is an example of the command:

wp search-replace 'http://example.dev' 'http://example.com' --precise --recurse-objects --all-tables

You can read more about the parameters available to you for the wp search-replace command in the official WP-CLI documentation, or check out this advanced search and replace WP-CLI guide.

And a fourth option, of course, would be to manually run the search and replace queries directly in MySQL. But this should only be done by professional WordPress developers.

Scroll to Top