Search and replace is a powerful tool for making mass updates to your WordPress database. This is especially helpful when updating URLs, such as changing from http to https, or when replacing specific words, phrases, or characters. This guide walks you through what search and replace is, why and when you might need it, and how to perform it safely using a plugin, WP-CLI, or phpMyAdmin.
What Is Search and Replace?
Search and replace involves locating specific text, URLs, or values in your WordPress database and replacing them with updated information. This process ensures consistent updates without manually editing individual entries, saving time and effort.
Why Do a Search and Replace?
Search and replace can help in many scenarios, such as:
Updating URLs from
httptohttpsafter enabling an SSL certificate.Switching URLs between
wwwand non-wwwformats.Replacing environment domain URLs with a primary domain after setting it.
Making mass updates to specific words, phrases, or values across your database.
Important: Always back up your database before performing a search and replace to prevent data loss or corruption.
How to Perform a Search and Replace
Using a Plugin
The easiest way to perform a search and replace is with a WordPress plugin like Better Search Replace. This method is beginner-friendly and requires no coding knowledge.
Steps:
Install the Plugin:
Go to your WordPress admin dashboard.
Navigate to Plugins > Add New, search for Better Search Replace, and click Install Now followed by Activate.
Run the Search and Replace:
Navigate to Tools > Better Search Replace.
In the Search for field, enter the old value (e.g.,
http://example.com).In the Replace with field, enter the new value (e.g.,
https://example.com).Select the tables to update. Usually, selecting all tables is appropriate.
Perform a Dry Run:
Check the Run as dry run? option to preview changes without making updates.
Execute the Search and Replace:
Uncheck the dry run option and click Run Search/Replace.
Verify the changes have been applied successfully.
Use Case Example: Update all database entries from http://example.com to https://example.com.
Link to Plugin: Better Search Replace Plugin
Using WP-CLI
WP-CLI is a powerful tool for advanced users comfortable with the command line. It allows you to perform search and replace operations across your WordPress database efficiently.
Disclaimer: WP-CLI commands directly modify your database. Use caution and back up your database before proceeding.
Steps:
Access WP-CLI:
Connect to your server via SSH and navigate to your WordPress installation directory. On Rapyd, this is in
/var/www/webroot/ROOT.
Run the Command:
Use the following WP-CLI command to replacehttpwithhttps:wp search-replace 'http://example.com' 'https://example.com' --all-tables --precise --recurse-objects --skip-columns=guid
Explanation of flags:
--all-tables: Searches all database tables.--precise: Ensures accurate replacements.--recurse-objects: Updates serialized data, preserving integrity.--skip-columns=guid: Avoids updatingguidfields, which should remain unchanged for RSS feeds and post links.Preview Changes:
Add the--dry-runflag to simulate the changes without applying them:wp search-replace 'http://example.com' 'https://example.com' --all-tables --precise --recurse-objects --skip-columns=guid --dry-run
Apply Changes:
Once satisfied, run the command without the--dry-runflag to apply the changes.
Use Case Example: Transition your site from http to https after enabling SSL.
Link to WP-CLI Documentation: WP-CLI Search and Replace
Using phpMyAdmin
For those who prefer working directly with the database, phpMyAdmin allows you to execute a MySQL query for search and replace.
However, the SQL query provided below is not completely safe for all cases in WordPress because it does not handle serialized data properly, except for very specific fields like siteurl and home in the wp_options table. Direct SQL queries should be used only when you're absolutely sure that you're not modifying serialized data.
Direct SQL Query Caveats
Risk of Breaking Serialized Data: WordPress uses serialized data for storing arrays and objects, especially in fields like
meta_valueandoption_value. A simple SQLREPLACEstatement will not adjust the length of serialized strings, leading to data corruption.Limited Safe Usage: The provided SQL query is safe only for specific fields that do not store serialized data, such as
siteurl,home,guid, or plain text fields inpost_contentorcomment_content.
We do not recommend this option unless you are an experienced WordPress developer. Even if that is the case, we strongly recommend WP-CLI. Make sure to backup your database before continuing!
Steps:
Access phpMyAdmin:
Log into your hosting dashboard and navigate to Settings > Database > phpMyAdmin.
Backup Your Database:
Before making changes, export your database as a backup from phpMyAdmin. Learn how to do this in this guide: How to Import and Export Your Database with phpMyAdmin
Locate Your Database:
Select your WordPress database from the list on the left.
Run SQL Queries:
If you absolutely must run a raw SQL query in phpMyAdmin, here's a modified approach to avoid altering serialized data. Use queries only for non-serialized fields, such as updating URLs in the
wp_postsandwp_commentstables:UPDATE wp_options SET option_value = REPLACE(option_value, 'http://example.com', 'https://example.com') WHERE option_name IN ('siteurl', 'home');UPDATE wp_posts SET guid = REPLACE(guid, 'http://example.com', 'https://example.com');UPDATE wp_posts SET post_content = REPLACE(post_content, 'http://example.com', 'https://example.com');UPDATE wp_comments SET comment_content = REPLACE(comment_content, 'http://example.com', 'https://example.com');
Important Notes:
Replace
http://example.comwith your old URL andhttps://example.comwith the new URL.Adjust the table prefixes (
wp_) if your database uses a custom prefix.This query is not safe for fields that store serialized data (e.g.,
meta_valueinwp_postmetaorwp_usermeta).Always back up your database before running the query.
Test Your Site:
Verify that all links, images, and resources have been updated correctly.
For a safe search and replace, prefer WP-CLI or the Better Search Replace plugin. Direct SQL queries are fine for very specific use cases (e.g., updating siteurl and home values), but they should never be used on serialized fields.
Conclusion
Performing a search and replace in WordPress is an essential task when migrating a site, changing a domain, or making bulk content updates. By using a plugin, WP-CLI, or phpMyAdmin, you can quickly and safely update your database. Always remember to back up your database before making changes, and test your site thoroughly afterward.
For further assistance, contact our support team or explore additional resources in our knowledge base.
Let me know if you'd like further adjustments! π