How to Update WordPress URLs
When changing your WordPress site's domain name or moving it from a staging site to production, updating URLs is essential to ensure all links and resources point to the correct location. Here are two methods you can use: one with a plugin and one using WP-CLI for command-line users.
Option 1: Using the Better Search Replace Plugin
- Install the Plugin
- In your WordPress dashboard, go to Plugins > Add New.
- Search for Better Search Replace. (Manual Install Link)
- Install and activate the plugin.
- Run a Search and Replace
- After activation, go to Tools > Better Search Replace.
- In the Search for field, enter your old URL (e.g.,
staging.yoursite.com
). - In the Replace with field, enter the new URL (e.g.,
yoursite.com
). - Select the database tables to update (typically all tables).
- Dry run first to check how many fields will be changed without making actual modifications.
- If the results look good, uncheck the "Dry Run" box and click "Run Search/Replace."
- Clear Cache
- After completing the replacement, clear any cache (both on your website and server) to ensure changes reflect immediately.
Option 2: Using WP-CLI Command via cPanel Terminal (SSH)
If your package includes SSH, you can use the cPanel Terminal to access SSH and run WP-CLI commands.
- Enable SSH in cPanel:
- Login to your cPanel dashboard.
- Look for the Terminal option under the Advanced section.
- If it's your first time using the Terminal, click "Proceed" to activate it.
- Access Your WordPress Directory:
- Once the terminal is open, navigate to your WordPress root directory (usually
/public_html/
) by entering:
cd public_html
- Once the terminal is open, navigate to your WordPress root directory (usually
- Run the URL Update Command:
- Use the following WP-CLI command to update URLs across your WordPress database, this will replace all occurrences of your old URL with the new one in every table of your database:
wp search-replace 'staging.yoursite.com' 'yoursite.com' --all-tables
- Preview Changes (Dry Run):
- If you want to preview the changes before making them, use the dry run option:
wp search-replace 'staging.yoursite.com' 'yoursite.com' --dry-run
- This will show you how many changes will be made without modifying the database.
- Clear Cache:
- After running the command, clear your website cache (if any) to ensure the changes take effect immediately.