Running a Search and Replace in Your WordPress Database
Running a Search and Replace in Your WordPress Database
WordPress stores URLs and text in many database tables. After a domain change, SSL migration, or copying staging to production, old URLs or text can remain in the database and cause broken links, mixed content warnings, or incorrect redirects.
Always take a backup before running a search-replace. KPanel creates a pre-update backup automatically when you use the built-in Search & Replace action, but if you are running WP-CLI commands manually, create a backup first from the Backups tab.
When You Need Search and Replace
- Migrating from
http://tohttps://(SSL setup) - Changing your domain (e.g. old-domain.com to new-domain.com)
- After pushing staging to production (staging URL left in the database)
- Updating a URL used across many posts (e.g. old image CDN URL)
Method 1: KPanel Search & Replace (built-in)
KPanel runs WP-CLI search-replace through the WordPress Quick Actions panel.
- Go to your site in KPanel
- Click WordPress in the top navigation
- On the Quick Actions tab, find the Search & Replace card
- Click Configure
- Enter the text to find in the Find (old value) field
- Enter the replacement in the Replace with field
- Click Run
- Confirm the dialog (a backup is created automatically first)
This runs wp search-replace across all tables using WP-CLI.
Method 2: WP-CLI via the Console (more control)
Use the Console tab for more control, including a dry run to preview changes first.
- Go to your site in KPanel
- Click WordPress in the top navigation
- Click the Console tab
Dry run first (no changes made):
search-replace 'http://old-domain.com' 'https://new-domain.com' --all-tables --dry-run
This shows how many replacements would be made in each table without making any changes. Review the output before proceeding.
Run the actual replacement:
search-replace 'http://old-domain.com' 'https://new-domain.com' --all-tables
Note: in the Console tab you type commands without the wp prefix -- KPanel adds it automatically.
Common flags:
| Flag | What it does |
|---|---|
--all-tables | Searches all tables including custom plugin tables, not just core WP tables |
--dry-run | Previews changes without writing anything to the database |
--precise | Uses PHP for serialisation handling instead of SQL (slower but handles edge cases) |
Method 3: phpMyAdmin (manual)
Use this only if WP-CLI is unavailable. You will need to update multiple tables manually.
Key tables that hold URLs:
| Table | Columns |
|---|---|
wp_options | option_value (rows: siteurl, home) |
wp_posts | post_content, guid |
wp_postmeta | meta_value |
- Open phpMyAdmin (KPanel: Files menu, then Database)
- Click the SQL tab
- Run a query for each table:
UPDATE wp_options
SET option_value = REPLACE(option_value, 'http://old-domain.com', 'https://new-domain.com')
WHERE option_name IN ('siteurl', 'home');
UPDATE wp_posts
SET post_content = REPLACE(post_content, 'http://old-domain.com', 'https://new-domain.com');
UPDATE wp_posts
SET guid = REPLACE(guid, 'http://old-domain.com', 'https://new-domain.com');
UPDATE wp_postmeta
SET meta_value = REPLACE(meta_value, 'http://old-domain.com', 'https://new-domain.com');
Warning: phpMyAdmin's REPLACE() function does not handle PHP serialised data correctly. Serialised strings (used by many plugins) store string lengths, and a manual SQL replace will corrupt those entries. Use WP-CLI where possible, as it handles serialised data correctly.
After Running Search and Replace
- Clear your WordPress cache: WordPress tab, then Flush Cache
- Visit your site and check for mixed content warnings (the browser padlock)
- Check your site's admin area: Settings, then Permalinks, then click Save Changes (re-flushes rewrite rules)
- If using a caching plugin, clear its cache from the plugin settings
