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:
    wp search-replace '' ''
  • 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:
    UPDATE wp_options SET option_value = replace(option_value, '', '') WHERE option_name = 'home' OR option_name = 'siteurl';
    UPDATE wp_posts SET guid = replace(guid, '','');
    UPDATE wp_posts SET post_content = replace(post_content, '', '');
    UPDATE wp_postmeta SET meta_value = replace(meta_value,'','');

Note: Replace ‘’ and ‘’ 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.