How To Replace A Word In WordPress Posts

In the previous post, you created your very first WordPress plugin. Now let’s make it do something!

How about changing occurrence of a particular word in posts/pages with something else?

For instance, let’s replace pluto with pluto (not a planet). It can be achieved by adding the code below to the plugin file you created in the last post:

function replace_pluto( $text ) {
    return str_replace( 'pluto', 'pluto (not a planet)', $text );
add_filter('the_content', 'replace_pluto');

That’s all.

What does it do? This code creates a function called replace_pluto and hooks that function into a filter called the_content.

It means whenever the_content is displayed on the front page, it will trigger our function replace_pluto and return the modified text.


Similarly, we can also capitalize the first letter of each word by creating a new function and hooking it into the_title filter:

function uc_post_titles( $title ) {
	return ucwords( $title );

add_filter( 'the_title', 'uc_post_titles' );


WordPress hooks (i.e. actions and filters) are quite powerful and you’ll use them a lot in WordPress development. You can learn more about them here.

How To Create A WordPress Plugin In 10 Minutes

There are plenty of similar posts available all over the internet so I’ll try to keep this short and to the point.

Local Developer Environment

To create a WordPress plugin, first you’ll want to setup a WP developer environment. There are many options but I recommend using VVV (Varying Vagrant Vagrants).

The installation is quite simple. First install VirtualBox and then clone the VVV repository in your local machine; let’s say to a folder called vagrant-local. Then cd into it and type in vagrant up.

The three commands I mentioned are listed below:

git clone -b stable git:// ~/vagrant-local

cd ~/vagrant-local

vagrant up

It will take a while to execute if you’re running it for the first time. It could fail because of network issues. If it does, just try again with vagrant up.

Once it finishes executing, you shall be able to load the two (default) local test sites at one.wordpress.test and two.wordpress.test in your web browser.

Create your first WordPress plugin

Now that you have a local WordPress development environment ready, you can start creating your very first WordPress plugin.

Open the plugins folder in one of the two test sites. For the first test site, it should be located at the following path:


Create a new folder inside it. Let’s call it Mars for no reason at all (or you could just name it whatever you like).

Now create a file called mars.php inside this new folder. We need the file-name to match the folder-name.

Paste the following content inside it:

* Plugin Name: Mars
* Plugin URI:
* Description: I'm your first plugin. I'm also the 4th planet from the Sun in our solar system.
* Version: 1.0
* Author: Your Name Here
* Author URI:

Save the file.

Activate the plugin

Open the wp-admin dashboard in one.wordpress.test site > go to plugins and you shall see your first plugin in an inactive state:

Activating it does nothing at the moment but it should help you understand the code above.

That’s all for now. Let’s make it do something in the next post.