Enhance Your Self-Hosted WordPress Site By Writing Your Own Plugin – Part 1

This entry is part 1 of 1 in the series wordpress plugin
  • Enhance Your Self-Hosted WordPress Site By Writing Your Own Plugin – Part 1

If you have a self-hosted WordPress powered site, I’m sure you’ve probably already installed some plugins to give you some extra functionality.

You may have even searched for a plugin that would let you do what you want, but you can’t find one? This is reasonable, given that plugins are by their very nature generic, and provide functionality to the widest possible audience.

If you can’t find what you need, its really not that difficult to write your own (and I like this, because it fits in nicely with the do-it-yourself theme of this site!) :)

Some background:

I’ve been creating a custom plugin over the past few weeks, and I thought I’d write a tutorial on how to start writing your own.

I’m almost a complete noob when it comes to PHP (the language WordPress is written in) but having a programming background, its not too difficult for me to understand – but I will show you a couple of things that have tripped me up – remember these!

You on the other hand, may have never coded in your life, so I’ll assume that to be the case. If you do have some experience with PHP, please leave a comment and let me know how I went! :)

Obviously, this tutorial will be generic as well, since I can’t possibly write a tutorial that might cover every single aspect of functionality that you might want in your own plugin, but what I will be covering are:

Some important aspects:

the initial plugin file – all plugins must have at least this file, and it must be written in such a way that WordPress shows it in the list of available plugins, which it then gives you a chance to activate, as you will have seen with all the plugins you may have downloaded and installed. You have to activate them to make them do something. We need this file to actually give your brand new plugin life, so we’ll (need to) do that first.

content replacement – this fragment will let you see that your plugin actually does something – that’s the start of the journey.

options – for a simple plugin, options may be just what you need – check a few option boxes, enter a piece of text, and you can change the way your plugin operates. We’ll have a look at options because WordPress has good support for setting and reading options – no need to reinvent the wheel, but by using WordPress options support, you’ll see that your plugin actually does something!

back-end admin menu – to actually provide you with a way to change the way your plugin operates, you’ll need a back-end admin page that lets you get and set options for your plugin.

database integration – some plugins can get away with setting simple options that determine how a plugin operates. But some plugins can get very complex, and need additional tables in the WordPress database to store their required data. WordPress gives you all the tools needed to create tables, and insert and retrieve data from those tables, using the same set of inbuilt database support objects that WordPress provides. This is a bit advanced, but stay with me, because it really isn’t that hard.

Throughout these tutorials, you’ll be given the exact copy/paste code that you can use to generate your very own plugin – you won’t have to do your own research as to how to create a WordPress plugin – because all the code you need will be presented below.

Why yet another WordPress plugin tutorial?

While there exists an abundance of websites and tutorials about how to create a plugin for WordPress out there on the interwebs, they just give snippets here and there – without telling exactly where to put those snippets, and in what context should they be used. Well, that was my experience anyway…. :) (Maybe I was using the wrong search keywords in Google – but I digress….)

To recap before we start:

The main things that you’ll need for a functional WordPress plugin are:

  • create the main plugin file – this is needed for any plugin you want to produce.
  • content replacement snippet – this is the best way to see that your plugin is actually working.
  • options – now that you have your plugin working through content replacement, options lets you change what text gets replaced.
  • back-end admin menu – the place that lets you get and set the options that change what your plugin does.
  • database integration – this is advanced work, but it builds on what you’ve learnt before.

Walk before you run.

First things first though. (If you’ve read the other lessons on this site, you’ll have read that I always say that, don’t I?) :)

Stop reading this blog and go and learn something about programming for the web. :)

This page has such good information, it’d be silly of me to re-iterate it here. When you’ve finished reading that, come back here. I’ll be waiting :)

Ready? Lets get started.

Here’s your brand new plugin code – copy / paste this into a text editor, and save it to a file called – well, maybe for now, until you get a feel for how it works, call it my-first-plugin.php – but remember to add a .php extension at the end.

Plugin Name: DIYWS Test Plugin
Plugin URI: http://diyws.ath.cx/
Description: Example WordPress plugin for DIYWS
Author: Brad Thomas

Version: 1.0
Author URI: http://bradzo.ath.cx

function my_content_replacement_function($content_from_wordpress) {

$content_we_want_to_replace = "xxxxx";

$my_content = "Hi There!";

return str_replace($content_we_want_to_replace, $my_content, $content_from_wordpress);
add_filter('the_content', my_content_replacement_function);

Then, in your WordPress sites’ wp-content/plugins folder, create a new folder – this is where your new plugin will live. Upload via FTP, or copy into it if local, the file you just saved.

That’s all you need. This plugin will actually do something that you can see.

Try it out!

Go to your WordPress admin dashboard, click Plugins, and you’ll see your plugin that you can activate.

Go ahead and activate it. Nothing will happen, just yet.

Next step:

Go and create a new page (or post) and type five x’s somewhere in it, as in:

“Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea xxxxx commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.”

(Thanks to the page at http://www.lipsum.com/ for the above text)

Save the page, then ask WordPress to view it.

Voila! If you’ve done everything correctly up to this point, you should see “Hi There!” where your five x’s should have been! (And if you noticed that there are no x’s in the above paragraph, give yourself a pat on the back! But… can you explain why?)

Congratulations! You’ve just created your very first WordPress plugin.

How did you go? Did it work?

I’ve been writing this post over a few days, and have tested this on my own site and it works, so if you get errors, and your WordPress installation doesn’t load properly, simply delete your plugin folder from wp-content/plugins and WordPress will deactivate your plugin automatically. Then you can start again – please let me know if you have troubles, because you’ll need to at least get this working before you can progress any further.


And to test that it worked (on my site) check this out – WordPress plugin tutorial test page.

Stay tuned for Part 2, where’ll we’ll create an admin page, and let you change the hard-coded “Hi There!” text, to something of your own choosing, by utilising WordPress’ in-built options settings.


You’ll have noticed that in the lorem ipsum text above, I’ve bolded the five x’s. For your homework, what would you need to change to the $my_content  variable in order to display those five x’s in bold?

The answer will be revealed in Part 2, where I’ll show you just what each line in your plugin file does.

In Summary:

WordPress is a great platform for building almost any type of website that you care to imagine – the possibilities are limitless when you can simply add extra code (via plugins) to the base WordPress installation.

So what did you learn today?

1. Every WordPress plugin needs at least one .php file, and it must be written in such a way that WordPress recognises it as a plugin, and gives you the chance to activate it.
2. To see that your new plugin actually does  something, we’ve called a WordPress function called add_filter and passed it the name of our content replacement function, where the actual work gets done.

Comments are of course, welcome!

You're invited to add your thoughts, comments and suggestions


Powered by Facebook Comments

4 Comments to Enhance Your Self-Hosted WordPress Site By Writing Your Own Plugin – Part 1

  1. [...] Links ← Enhance Your Self-Hosted WordPress Site By Writing Your Own Plugin – Part 1 [...]

  2. [...] that reminds me – I wonder if there is a way that this could be integrated into a WordPress plugin – imagine the scenario – write the plugin to use the StumbleUpon API – grab the [...]

  3. gain likes of photo through facebook says:

    Currently it seems like BlogEngine is the best blogging platform available right now. (from what I’ve read) Is that what you’re using on your blog?

Leave a Reply