X

How to Replace Keywords in the_content and the_excerpt

Snippets by IsItWP

Are you looking for a way to replace keywords within the_content and the_excerpt using an associative array? While there’s probably a plugin for this, we have created a quick code snippet that you can use to replace keywords in the_content and the_excerpt in WordPress.

Instructions:

All you have to do is add this code to your theme’s functions.php file or in a site-specific plugin:

function replace_text_wps($text){
	$replace = array(
		// 'WORD TO REPLACE' => 'REPLACE WORD WITH THIS'
		'wordpress' => '<a href="#">wordpress</a>',
		'excerpt' => '<a href="#">excerpt</a>',
		'function' => '<a href="#">function</a>'
	);
	$text = str_replace(array_keys($replace), $replace, $text);
	return $text;
}

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

Note: If this is your first time adding code snippets in WordPress, then please refer to our guide on how to properly copy / paste code snippets in WordPress, so you don’t accidentally break your site.

If you liked this code snippet, please consider checking out our other articles on the site like: 19 best WordPress plugins for photographers and how to increase maximum file upload size in WordPress.

Comments   Leave a Reply

  1. Hello, made an alteration of it like this but can’t get any effect:

    function replace_text_wps($text){
    $replace = array(
    // ‘WORD TO REPLACE’ => ‘REPLACED WITH THIS’
    ‘spicy’ => ”,
    ‘veryspicy’ => ”
    );
    $text = str_replace(array_keys($replace), $replace, $text);
    return $text;
    }

    add_filter(‘the_content’, ‘replace_text_wps’);

    I added this in the end of the theme functions.php but I can’t get any effect.

  2. WordPress Arena: A Blog for WordPress Developers, Designers and Blogger August 27, 2011 at 4:01 pm

    […] code is courtesy of Kevin Chard from WpSnipp, and we should be thankful to […]

  3. The Best way to replace words in your posts # WordPress Tricks & Tips April 4, 2011 at 8:45 am

    […] On the code above using add_filter to filter the_content and the_excerpt. So all match words will be replace with the new parameter as described. That’s is, we hope this tutorial works on you. Thanks Kevin Chard from WP Snipp for this code […]

  4. Kako automatski zameniti izraze | WPSavetnik March 18, 2011 at 1:52 pm

    […] odlomak koda preuzet je sa sajta wpsnipp.com, odli?ne kolekcije PHP caka i trikova za […]

  5. You have to be careful of this if you intend to use those keywords on an image caption, as it seems to break the caption. That’s too much of a drawback for me to use on common keyword, but otherwise this is really great. I’ll certainly find a use for this. Thanks!

    1. Hi Sean, yes this is true it will replace any text, but I’m glad you like the snippet. I’m sure that I will update it in the future to be a little more practical.

  6. If the word already is in a link, it will be some problems. My best, and simplest, solution for that is to replace eg. ‘ wordpress ‘ so that every appearence of wordpress with spaces on each side will be replaces.

    1. Hi Slowmove, this is true and is something people should take into consideration when using this snippet. Thanks for the suggestion this would work great for some instances. I may have to post an updated version in the future.

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!