A Smart Way to Replace Words or Terms in WordPress

A Smart Way to Replace Words or Terms in WordPress

Introduction

Suppose there is a special word that appears on many places in the content of your website. It could be:

  • the company name
  • a nickname
  • a band
  • a certain phrase
  • etc.

and if you need to change it on every single place, post or page in your WordPress site it would be a heavy work. If you still would have managed it, but after some time you change your mind or realize that those word has to be changed again for some reason, would you feel very smart then?

Well, don’t worry as exist a better way. On the following sections I’m going to talk about how to replace those words in a smart and professional way.

How to Filter a Word in The Content?

As you probably noticed, here I said “filter” instead of “change” a word. The reason is that now we are going to use the WordPress hook add_filter to finish our job.

Open your file functions.php which is in the root directory of your WordPress theme and put this peace of code:

<?php
add_filter('the_content', 'swrw_filter');

function swrw_filter($content) {
    $old_word = 'our-cool-band';
    $new_word = 'the-best-band';

    $content = str_ireplace($old_word, $new_word, $content);

    return $content;
}
?>

Here we see the use of add_filter. Before showing the content in the browse a filtering process is activated. The callback function swrw_filter gets fired (swrw is our prefix. You should always prefix your functions as they have to be unique. In our case the prefix swrw comes from A Smart Way to Replace Words.).

  • $old_word is our target word going to be searched and replaced.
  • $new_word is the new value of the word going to be replaced.
  • $content as the argument of swrw_filter function represents the content of all posts and pages where the search is going to be done.

Don’t forget to return the new $content which already holds the content with ‘the-best-band’ instead of ”our-cool-band”.

Another thing to pay attention on is that we intentionally used the function str_ireplace() as it is for case-insensitive replace. For case-sensitive replace you can use str_replace().

Is It Possible to Filter Many Words at Тhe Same Time?

Yes, it is possible by using arrays.

<?php
add_filter('the_content', 'swrw_filter');

function swrw_filter($content) {
    $old_words = array(
        'our-cool-band',      // will become 'the-best-band'
        'my-old-nickname',    // will become 'my-new-nickname'
        'my-old-company-name' // will become 'my-new-company-name'
    );
    
    $new_words = array(
        'the-best-band',
        'my-new-nickname',
        'my-new-company-name'
    );

    $content = str_ireplace($old_words, $new_words, $content);

    return $content;
}
?>

How to Filter a Word in The Title?

Well, it’s pretty straightforward. If you want to filter your titles simply put the_title instead of the_content:

<?php
add_filter('the_title', 'swrw_filter');
...
?>

You can have a look at the filter reference as there are also many.

Summary

I think that now we can be called smart people and do you know why? Because all the replacement done here didn’t touch our database at all. I mean that the original content is still the same though it’s not sent to the browser. So you can always remove the filter and changes will be rolled back, whenever you want.

Hm, why don’t I create a plugin for such a filtering and share it with the open community. It could be with some options that an user might choose from.

Hey, smart people, hope you liked the tutorial and comments are alsways welcome! :)

[Edited on 27 August, 2014]

There you go, I made that sort of plugin. I gave it the name of “GSY Content Filter”. You can find it on:

Resources

FacebookTwitterGoogle+LinkedIn

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>