WordPress Code Snippets

A fresh mix of code snippets and tutorials

Replace all instances of a string in WordPress

51

Here is how to replace all instances of a string in WordPress.

Just add the following code to your theme’s functions.php file:

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

This should be self-explanatory, just replace the strings with your own. Note that nothing is actually changed in the database — it’s only changed when output to the browser.

More info on http://php.net/manual/en/function.str-replace.php

Learn more

WordPress Themes In Depth


Show Support

Like our new Facebook Page to show support!