Replace a string in the whole WordPress database
Sometimes, because of a company rebranding or simple inattention, a string has to be replaced everywhere on the site, or a symbol added to a word. In my case “Company Name” had to become “Company Name®”. Traditionally, there are several ways to do it.
Problem: Replace a string or add a symbol on the whole WordPress site.
Solution: Replace it in the database with SQL, or in the SQL dump with sed. Back up the database first, and read the note about serialized data below.
1. SQL REPLACE()
Replace the string in the columns where the content is: post_content (posts, pages and custom posts), meta_value (custom fields, ACF), option_value (options, ACF option pages):
UPDATE wp_posts SET post_content = REPLACE( post_content, 'Company Name', 'Company Name®' ); UPDATE wp_postmeta SET meta_value = REPLACE( meta_value, 'Company Name', 'Company Name®' ); UPDATE wp_options SET option_value = REPLACE( option_value, 'Company Name', 'Company Name®' );
Titles and excerpts are in post_title and post_excerpt of the same table, if the string can be there too.
2. sed on the dump
This way replaces the string in the SQL file, for example before importing it on another server. Be careful not to replace permalinks, slugs and so on by accident.
sed -i 's/original/new/g' database.sql
where:
sedis the Stream EDitor;-imeans in-place, the result is saved back to the same file;sis the substitute command;originalis a regular expression for the text to replace (or just the word itself);newis the text to replace it with;gis global: replace all occurrences, not only the first one in each line;database.sqlis the file name.
On macOS, sed -i needs an empty argument: sed -i '' 's/original/new/g' database.sql.
If the string contains spaces, like “Company Name”, wrap it in double quotes inside the single-quoted command:
sed -i 's/'"Company Name"'/'"Company Name®"'/g' database.sql
Serialized data
Plugins and themes (including ACF and widget settings) often store arrays in wp_options and wp_postmeta as serialized PHP strings, which contain the length of every value: s:12:"Company Name";. “Company Name®” is 14 bytes in UTF-8, so after a plain replacement the length is wrong, PHP cannot read the value, and the setting is lost. Both ways above have this problem.
The safe way for these tables is WP-CLI, which unserializes the values, replaces the string and serializes them back:
wp search-replace 'Company Name' 'Company Name®' --dry-run wp search-replace 'Company Name' 'Company Name®'
--dry-run only shows how many replacements would be made in each table. Without WP-CLI, the Better Search Replace plugin does the same from the admin.
Update (September 2021): Sometimes we need to replace the site URL, for example before importing the database from a development server to the live one. A URL contains slashes, so use another delimiter in the sed command, for example #:
sed -i 's#https://dev.example.com/#https://example.com/#g' database.sql
The serialization note applies here too: if the old and new URLs have different lengths, wp search-replace is the reliable way.
Sources: