Better Search Replace

Mr. Ollivander once told Harry Potter that “After all, He-Who-Must-Not-Be-Named did great things. Terrible! Yes. But great.” Better Search Replace is like that. It’s dangerous, but powerful. If you use it responsibly, it’ll be a useful ally.

Better Search Replace lets you find bits of text in the WordPress database and replace them with other bits of text.

What you have to understand here is that it searches the entire database. Not just the content, but every other field as well. If you replace the wrong thing, it could knock a foundational part of your website loose. And we don’t like it when that happens.

How to Use Better Search Replace Safely

  1. Always do a “dry run” first. This just runs the search, and doesn’t replace anything.
  2. Keep your searches focused. Make your search terms highly specific.
  3. Try to think of variations to your search. Example: you’re trying to replace all <h2> tags with <h3> tags. Some <h2> tags are written plain, while other ones have a class on them, like <h2 class="special">. The best way to get rid of both the plain and special <h2> tags is to run two searches.
  4. Only select the tables you need to search in. If you’re looking for post content, search in wp_posts and wp_postmeta. If you’re looking for a site option, search in wp_options.
  5. Check your results first, then do the replace for real.

Remember, this can break your site if you’re not careful! For example, be careful if you’re replacing URLs. WordPress stores the URL for the site and the home page in wp_options: messing those up will break your both the public and admin parts of your site.  You won’t be able to fix it through the WordPress admin. It can only be fixed through phpMyAdmin.

Better Search Replace Examples

To use Better Search Replace, install the plugin and then go to the Admin Screen through Tools -> Better Search Replace. Here are a few examples of how you could use Better Search Replace on your site:

  1. Locate hard-to-find content. You can see a line of text on the front-end of your site, but you’re not sure where it is in the admin. Searching for that specific line could help you replace it, without necessarily knowing where it is.
  2. Switch out tags in bulk. Header tags (like <h2>) are important for SEO. It’s normal for us to get a site that has a lot of unoptimized header tags. If you need to switch out a lot of them in bulk, Better Search Replace is a good way to do it.
  3. Remove junky markup from a bad copy-and-paste. Pasting in content from Word, Google Docs, Sketch, etc, often inserts extra markup, like <span class=”p1″>. WordPress sometimes inserts line-height styles (e.g., <li style=”line-height:1.8″>) at weird places too due to either a browser bug or a tinyMCE bug.

I haven’t tried the PRO version of this, but the features list looks very useful. I’d love to save a profile for replacing that line-height bug, so that all I have to do is go to the tool and run the saved search-replace, rather than typing it out again.

Got another way you like to use Better Search Replace?

I’d love to hear about it! Comment, email or tweet @wpeditorpro, and I’ll throw it on here. Together we can edit WordPress faster.