How to replace a Sentence on multiple posts [WordPress]

How to replace a Sentence on multiple posts [Wordpress]
How to replace a Sentence on multiple posts [Wordpress]

Do you have a sentence that occurs multiple times on several posts and wanna change that sentence?

I will show you have to replace all instances of a string in WordPress.

First, you need to access your websites file manager and locate the functions.php folder under:

  1. wp-content
  2. themes
  3. the theme currently in use
  4. here you should find functions.php

When you have found that, you’ll add the following code

function replace_text($text) {
	$text = str_replace('look-for-this-sentence', 'replace-with-this-sentence', $text);
	$text = str_replace('look-for-that-sentence', 'replace-with-that-sentence', $text);
	return $text;
}
add_filter('the_content', 'replace_text');

So this should be straightforward, you’ll add the sentence you wanna replace in the “look-for-this-sentence’, and what it needs to be replaced for in the “replace-with-that-sentence”.

It’s a good thing to note that nothing is actually changed in the database, it’s only changed when shown to the browser.