DISCLAIMER: this post is older than one year and may not be up to date with latest WordPress version.

Imagine your blog was named “myblog” and you renamed it “mysuperblog”. Don’t edit your XXX posts to replace every single occurence! This extremely useful tip will do it for you, in just a minute!

Simply put words to replace in the array on line 4. Once done, paste the code into your function.php file, save the file, and you’re done!

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');

Thanks to Kevin Chard for the cool tip!

2 Comments

  1. Won’t the above function replace words within links etc…?

    If you already had a link to WordPress with anchor text WordPress within the body of a post the end result would be:

    <a href="url"><a href="url">wordpress</a></a>

    Which is broken HTML.

    I’ve seen code somewhere that would not replace the text if it’s within specific tags, think it was from the SEO Smart Links plugin. That plugin is just for replacing word with links and I know it doesn’t replace words within specific HTML tags.

    David

  2. I thought replacing it by using a SQL would have been a better choice. Anyways, should this piece of code be removed after the executing it once?

Leave a Comment

Your email address will not be published. Required fields are marked *