How to Create a Simple WordPress Plugin

Creating a WordPress Plugin might seem like a complicated task, but it can be done in a few simple steps. Here’s how.

In your WordPress directory create a new directory under the “wp-content” -> “plugins” directory.

In the example above I’ve created a directory named “hwn-text-replacer”, once a directory has been created we need to add a file.

As you can see from the image above I’ve added a file named “hwn-text-replacer.php” but you can call the file and directory anything you wish so long at the names comply with the general guidelines.

Now we need to add a header to our php file, the header will let WordPress know some details about our plugin. Here’s the text I added to my php file.

<?php
/**
 * Plugin Name: Text Replacer
 * Plugin URI: https://www.hardworkingnerd.com/
 * Description: Replace text on wordpress pages
 * Author: Hard Working Nerd
 * Author URI: https://www.hardworkingnerd.com/
 * Version: 1.0.0
 */
?>

Once the header details have been added you should see your new plugin when you visit the “Plugins” page inside your theme, here’s what I see after adding the header above.

Good news! WordPress has recognized our new plugin, so lets add some functionality just to check everything. is working OK.

The Functionality

As the plugin name suggests we’re going to write some code that will replace words in WordPress content, the purpose of this article is really to show how to create a WordPress plugin so I don’t want to spend ages discussing the code. Here’s what we’ll use –

add_filter('the_content', 'hwnFixContent');

function hwnFixContent($content)
{
    $replacements = array(
        'quick' => 'hungry',
        'fox' => 'wolf',
        'jumped over' => 'ate',
        'dog' => 'mouse'
    );
    $content = str_ireplace(array_keys($replacements), $replacements, $content);
    return $content;
}

The codes hooks some additional functionality into the WordPress “the_content” filter.

To break this down briefly, the “the_content” filter provides us with the opportunity to change the content of a  WordPress page or post after it is retrieved from the database but before it is shown to the user.

For example, if you’d published thousands of articles that contained the word “color” but you wanted to aim the articles at a British audience you could use the content filter to change the spelling across your entire site rather than trawling through thousands of articles to make the change.

In our code this line –

add_filter('the_content', 'hwnFixContent');

tells WordPress that every time it is about to show the content to the user it should run a function called “hwnFixContent”. The hwnFixContent function looks like this –

function hwnFixContent($content)
{
    $replacements = array(
        'quick' => 'hungry',
        'fox' => 'wolf',
        'jumped over' => 'ate',
        'dog' => 'mouse'
    );
    $content = str_ireplace(array_keys($replacements), $replacements, $content);
    return $content;
}

The function takes a single parameter named $content, behind the scenes, WordPress will pass the content that has been taken from the database to us in this variable, and then we need to make our changes to it and then return the altered value.

In the function above we use the str_ireplace function to replace a number of words if they appear in the content of the current post, you can find more details about how the code works in this article.

So now with our plugin registered and code in place we can test our plugin to see if it works, to do this we’ll add some content to a new post as below.

content replace test

Now when we view the post we can see our replace code in action.

content with plugin

Notice how the text changed from what we originally entered in the post? That’s our plugin at work. To prove that everything s working as expected if we deactivate our plugin then the post will change to display it’s original content.

content without plugin

So we’ve written a plugin, added it to WordPress and seen it change some content. If you have any questions or queries I’d love to hear them in the comments.

Leave a Comment