Sometimes, don’t you just wish you could do a simple text search and replace in a wordpress document, as you would with a word processor?
You can, with one of the various plugins. But if the plugin searches within the site database, can this be dangerous?
A plugin may search inside your website database to perform the search and replace process.
It may be a global search and replace? This is where the search replaces everything (with your search terms) in your whole site, (from inside the database). This can be dangerous if text you didn’t want changed (or shouldn’t change) is then updated.
You can do a simple search and replace without a plugin. Just follow these simple steps:
You can do this easily, within an individual WordPress Post or Page, and you don’t have to start playing around with your site database to make the changes (as some plugins do?).
The First Solution:
The only thing missing from the first solution, is ‘Replace all’, (which is not always a good idea). But you can find and replace text repeatedly…
The second solution does do a search and replace globaly, but you have to use a code editor and cut and paste. Even though you are making the changes outside of WordPress, it’s still a simple and quick solution.
How you can Select Text in WordPress to Search & Replace?
We need to view all the text for a post or Page.
Go to your WordPress Post or Page that you want to change (I’m using the latest version with Gutenberg blocks).
Select the post or page you want to edit, and open it in edit mode.
On the far right hand side you will see three dots ‘Show more tools & options’.
Click on the ‘More tools & options’ (three dots) and select ‘Code Editor’ in the Editor section. This will allow you to edit the whole post or page all at once, rather than individual blocks (it shows the post or Page in the HTML format).
Use your browsers ‘Find’ tool (Ctrl+f) to search for the text you want.
Manually update the text.
If you need to replace more items, use copy and paste and the browsers ‘find’ search controls to go to the next item.
This allows you to only replace the items you want to change.
When complete, switch back to the ‘Visual Editor’.
And save the document.
Don’t forget you can undo any changes…
And that’s it. your done. You have just completed a search and replace…
How can you do a Global Search & Replace in WordPress:
You can do a global search and replace in WordPress (without a plugin). But you have to use a code editor and a bit of cut and paste. It’s not difficult, as I’ll explain how below:
Note:
You can use any text editor with search and replace to do this, but it is better to use a code editor to perform this task. If you don’t have one, there are plenty good free examples you can use, such as Visual Studio Code, or Atom Editor, etc.
Go to your WordPress Post or Page that you want to change (I’m using the latest version with Gutenberg blocks).
Select the post or page you want to edit, and open it in edit mode.
On the far right hand side you will see three dots ‘Show more tools & options’.
Click on the ‘More tools & options’ (three dots) and select ‘Code Editor’ in the Editor section. This will allow you to edit the whole post or page all at once, rather than individual blocks (it shows the post or Page in the HTML format).
Copy the HTML to a Code Editor:
Open your code editor and create a new blank document.
Select all the HTML text from your WordPress document (Ctrl+A) and copy it (Ctrl+C).
Paste (Ctrl+V) the copied HTML version of your WordPress Page or Post into your code editor.
Search and Replace (individually and globally) in the Code Editor:
Open the code editor search and replace.
Input the required search and replace text.
Initiate the search and check the text found is what you want to replace.
Select ‘Replace’ or ‘Replace all’ depending on your preference?
Paste the Code Back into WordPress:
When you’ve finished updating the document, select all (Ctrl+A) and copy the text (Ctrl+C).
Go back to the edited WordPress document and delete the original text.
Paste the updated text from the code editor back into the WordPress document.
Save (Update) and View the Page:
Change back to the Visual Editor (if required).
And save the document.
Don’t forget you can undo any changes…
You can now view the updated document.
And that’s it. You have just completed a global search and replace…