Search and replace in WordPress lets you find a specific text string across your site’s database and swap it with another, which is essential for tasks like changing a domain name, updating URLs, or fixing typos site-wide. You can do this safely with a plugin, the command line, or phpMyAdmin, and this article explains each method, the risks, and the best practices to avoid breaking your site.
What Search and Replace Actually Does
When you use the WordPress admin dashboard, you can edit individual posts and pages, but there is no built-in tool to search and replace text across your entire site. The content of your posts, pages, custom post types, and settings are stored in your database (usually MySQL or MariaDB) in tables like wp_posts, wp_postmeta, and wp_options. A search and replace operation directly modifies these tables, swapping every occurrence of a search string for a replacement string.
Common uses include:
- Changing your site’s URL from
http://oldsite.comtohttps://newsite.comafter a domain change or migration. - Updating internal links that point to an old domain or an outdated path.
- Replacing a company name or product name across all pages and posts.
- Fixing a recurring typo or updating a phone number in the footer or sidebar widgets.
Important: this is a database-level operation, not a simple text editor find-and-replace. It affects every row in the selected tables, including WordPress options, widget content, and plugin settings, not just the visible content of your posts.
Why You Need a Special Tool
WordPress stores data in a serialized format for arrays and objects (e.g., in the wp_options table). A simple SQL query like UPDATE wp_options SET option_value = REPLACE(option_value, 'old', 'new') can break serialized data because the length of the string is stored in the data itself. For example, s:11:"oldstring" means a string of length 11. If you replace oldstring with newstring (length 9), the serialized data becomes invalid, and WordPress may throw errors or lose settings.
Therefore, you need a tool that understands serialization and updates the length values accordingly. The three main methods are:
- WordPress plugin (e.g., Better Search Replace, Search & Replace by Inpsyde) – runs through the admin dashboard, handles serialized data, and is the easiest for most users.
- WP-CLI (command line) – a powerful tool for developers, using
wp search-replace, which also handles serialized data. - phpMyAdmin – direct database access with SQL queries, but you must use a special script or be extremely careful with serialized data.
Each method has its own safety considerations, which we’ll cover next.
How to Search and Replace with a Plugin
Using a plugin is the most accessible method for non-developers. Here’s a general step-by-step process (specific menus may vary slightly by plugin):
- Back up your database first. This is non-negotiable. Use a plugin like UpdraftPlus or your host’s backup tool to create a full backup of your database and files.
- Install and activate a search-replace plugin like Better Search Replace (free) from the WordPress repository.
- Go to the plugin’s settings page (usually under Tools or a dedicated menu).
- Enter the search string (what you want to find) and the replacement string (what you want to replace it with).
- Select the tables you want to search. If you’re not sure, choose all tables, or select only
wp_postsandwp_postmetafor content changes. - Decide whether to run a dry run. Most plugins offer a “dry run” or “test” mode that shows how many replacements would be made without actually changing anything. Always do this first.
- Run the dry run, review the results, then run the actual search and replace.
After the operation, clear any caching plugins (like W3 Total Cache or WP Super Cache) and possibly your browser cache to see the changes.
How to Search and Replace with WP-CLI
If you have SSH access to your server, WP-CLI is faster and more reliable for large sites. The command is:
wp search-replace 'oldtext' 'newtext' --all-tables-with-prefix
This command automatically handles serialized data. Key options include:
--dry-run– shows what would be changed without applying it.--precise– forces the replacement to be precise (not skipping serialized data).--recurse-objects– handles objects within serialized data (rarely needed).--all-tables-with-prefix– applies to all tables with your site’s prefix (defaultwp_).
Example with a dry run:
wp search-replace 'http://oldsite.com' 'https://newsite.com' --dry-run
Then run it without --dry-run to apply. Always back up before running the live command.
How to Search and Replace with phpMyAdmin
Using phpMyAdmin directly is riskier because it doesn’t handle serialized data by default. If you must use it, you have two options:
- Use the SQL tab with a simple REPLACE query – only safe for tables that do not contain serialized data (like
wp_postsandwp_postmetaare generally safe for content, butwp_optionsis not). Example:
UPDATE wp_posts SET post_content = REPLACE(post_content, 'old.com', 'new.com'); - Use a PHP script like the one from interconnect/it (the “Search Replace DB” script) that you upload to your server, run, then delete. This script handles serialized data correctly.
Warning: Never run a raw SQL REPLACE on wp_options without a serialization-aware tool, as it can corrupt your site. Also, always back up before doing anything in phpMyAdmin.
Common Mistakes and How to Avoid Them
Even experienced users make errors. Here are the most common pitfalls:
- Not backing up. A single mistake can break your entire site. Always have a full backup.
- Replacing too broadly. If you replace a common word like “site” with “website”, you might unintentionally change URLs, slugs, or plugin settings. Be specific.
- Forgetting serialized data. Using raw SQL on serialized fields can cause fatal errors. Use a plugin or WP-CLI.
- Not using a dry run. Always preview the number of replacements before committing.
- Replacing a string that appears in your replacement. For example, replacing “old” with “new” and then running it again would turn “new” into “newn”. Do not run the same operation twice unless intended.
- Changing the URL incorrectly. When moving from HTTP to HTTPS, include the protocol and domain correctly. Also, you may need to update the
siteurlandhomeoptions inwp_options, but these are often handled by the search-replace if you include all tables.
When to Use Each Method
Choosing the right method depends on your comfort level and situation:
- Plugin – Best for most users, especially if you have no command-line experience. It’s safe, handles serialized data, and offers a dry run.
- WP-CLI – Best for developers or large sites with thousands of posts, as it’s faster and can be scripted. Requires SSH access.
- phpMyAdmin – Only recommended if you know exactly what you’re doing and you’re comfortable with SQL. For serialized data, use the script method, not raw SQL.
If you are only changing a few posts, you can also do a manual edit in the WordPress editor, which is always the safest option.
Frequently Asked Questions
Will search and replace change my site’s URL in the database?
Yes, if you search for the old URL and replace with the new one across all tables, it will update the siteurl and home options in wp_options as well as any content that contains the URL. This is the standard way to update a URL after migration.
Can search and replace break my site?
Yes, if done incorrectly, especially with serialized data or without a backup. Using a plugin or WP-CLI with a dry run minimizes the risk. Always back up first.
Is there a built-in WordPress search and replace?
No, WordPress does not have a native search-and-replace feature for the entire database. You need a plugin, WP-CLI, or phpMyAdmin.
How do I search and replace in WordPress without a plugin?
You can use WP-CLI (if you have SSH access) or phpMyAdmin. For phpMyAdmin, use a SQL query for simple tables, or a serialization-aware script for options tables.
What is a dry run in search and replace?
A dry run shows you how many replacements would be made and where, without actually changing the database. It’s a safety feature to verify your search string is correct.
Can I undo a search and replace?
Only if you have a backup. There is no built-in undo. Restore your database backup to revert the changes.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Tongue Escape Codes: What They Are0.20 movements/s
- Claude Code Workflow Habits Worth Trying
- HDMI 2.1 Explained: Why It Matters for Gaming
- Veeam Plugin for VMware Cloud Director: How It Works
- Choosing a Cybersecurity Compliance Automation Platform
- Use Markdown in WordPress? Yes, Here’s How