How to search and replace URLs in WordPress

Home > Resources > How to search and replace URLs in WordPress

In WordPress, you can search and replace URLs using various methods. One common scenario where this is necessary is when you migrate your WordPress site from one domain to another. Here are a few methods to search and replace URLs in WordPress:

1. Using a Plugin: Better Search Replace:

  • Install and activate the “Better Search Replace” plugin from the WordPress plugin repository.
  • Navigate to “Tools” and then “Better Search Replace.”
  • Enter the old URL and the new URL in the respective fields.
  • Select the tables you want to search and replace within (usually, it’s safe to select all tables).
  • Check the option to run as a dry run to see the changes before applying.
  • Once you’re satisfied, uncheck the dry run option and click “Run Search/Replace.”

2. Using WP-CLI:

  • If you have command line access, you can use WP-CLI for a more powerful and efficient way to perform search and replace.
  • Open your command line interface.
  • Navigate to your WordPress installation directory.
  • Run the following command:
    (bash)
    wp search-replace 'oldurl.com' 'newurl.com'
  • This will perform a dry run. If everything looks good, you can run the command again without the --dry-run flag.

3. Manually in the Database:

  • This method is riskier and should only be attempted if you’re comfortable working with databases.
  • Before making any changes, it’s essential to back up your database.
  • Access your WordPress database using a tool like phpMyAdmin.
  • Run SQL queries like the following:
    (sql)
    UPDATE wp_options SET option_value = replace(option_value, 'oldurl.com', 'newurl.com') WHERE option_name = 'home' OR option_name = 'siteurl';
    UPDATE wp_posts SET guid = replace(guid, 'oldurl.com','newurl.com');
    UPDATE wp_posts SET post_content = replace(post_content, 'oldurl.com', 'newurl.com');
    UPDATE wp_postmeta SET meta_value = replace(meta_value,'oldurl.com','newurl.com');

Note: Replace ‘oldurl.com’ and ‘newurl.com’ with your actual old and new URLs.

Remember to replace URLs carefully to avoid breaking links or causing issues with your site. Always perform a backup before making any significant changes to your WordPress site.