Find and Replace Words in a WordPress Post

This PHP function will search for a particular string of text in a WordPress post and replace it with alternative text of any kind.

If you’re just getting into the affiliate business, this is a great way of instantly monetising your blog via specific keywords. If you’ve ever written about Thesis, you could, via the function below, automatically convert every instance of the word “thesis” into an affiliate link.

Not unlike WordPress shortcode, it’ll permit you to globally change one piece of text into another. In this case, thought, we’re using the text itself rather than structured shortcode.

Loosely based on the principles applied in the post, “Pirate Day – WordPress Shortcode, we’re creating an array of both our original word and text to replace it. In fact, the Pirate post can be used almost as we’ve done; the different being that this function will retroactively alter defined words anywhere in your posts.

You’ll find far more useful plugins at WordPress.org… but this will be enough to get you started.

Copy the following code into your theme’s functions.php file.

function replace_custom_word($text){
    $replace = array(
	'thesis' => 'Thesis',
	'studiopress' => 'Studiopress'
    );
    $text = str_replace(array_keys($replace), $replace, $text);
    return $text;
}

add_filter('the_content', 'replace_custom_word');
add_filter('the_excerpt', 'replace_custom_word');

The function is case-SenSItiVe. If you would like to create a case-insensitive version of the above function, replace str_replace with str_ireplace.

The output

I included the function long enough on this site to take a screenshot. The above code will render the word “Thesis” as follows:

I’ve applied a basic style but you can obviously do whatever you want to it – including link titles etc.

Interested in researching the code?

If you’re interested in the code above, you’ll want to check out the following functions.

WordPress Functions

the_content : the_content displays the contents of the current post.

the_excerpt : the_excerpt displays the excerpt of the current post with [...] at the end, which is not a “read more” link.

the_filter : the_filter Filters are the hooks that WordPress launches to modify text of various types before adding it to the database or sending it to the browser screen. Plugins can specify that one or more of its PHP functions is executed to modify specific types of text at these times, using the Filter API.

PHP Functions

array, str_replace, array_keys


Download: WordPress Find & Replace
Description: Find and replace text (with anything) in a WordPress post.
Author:Marty
Category: PHP code
Date: October 27, 2011

If you liked this article, you may also like:

  1. Add WordPress tags within your post
  2. [Shortcode] Easily post an aviation Metar report into your post or page with shortcode
  3. Conversion Rate Optimisation opportunities for people near YOU – Geographic information in WordPress Titles
  4. Exclude posts from WordPress blog or RSS feed based on a tag
  5. [Shortcode] Easily post an aviation TAF report into your WordPress post or page with shortcode
About Marty

is a passionate web developer from Sydney, Australia. He owns about 400 websites and makes a healthy living from working the web. As a day job, he works as a pilot for an international airline. You can follow Marty on Twitter or Google+.

Comments

  1. eyesonstars says:

    You my friend are a bloody legend!! I had an issue in WordPress where I had to replace a lot of [shotcode]. I tried with sql- although it seems as though the [shortcode] doesn’t get placed in sql and I couldn’t find and replace them all in phpmyadmin… so I placed the [shortcode] that I need to replace into your .php code and then placed what I needed them to be changed in to- (basically) changed one shortcode into another using you text replace method. Worked a treat!! Thanks heaps for this.

Speak Your Mind

*