Introduction
Creating plugins for WordPress seems like a daunting task at first, but the process is actually quite simple. WordPress’ APIs are very elegant and make plugin development a breeze.
The documentation for writing a plugin can be found here. Make sure you bookmark it as you will be referring to the documentation quite a bit as you require more functionality.
WordPress Actions And Filters
When we talk about creating a plugin for WordPress, it usually refers to extending the functionality. This could be something as simple as replacing all of the colon-parens with smilies or as complex as doing complete SEO optimization. Whatever you want to create, chances are you will have to hook into a WordPress Action or Filter.
WordPress Filters are called any time there is data. They are essentially a chain of functions that will be called in order to work on the data in some way. For example, when WordPress displays the content of your post, rather than it saying <?php echo $content; ?>, it calls the function <?php the_content(); ?>. Calling this function will invoke the chain of functions to be applied to the content.
To hook a function into this chain, you will use the add_filter function of WordPress.
add_filter('the_content','myFunc'); function myFunc($content) { // Do Something to the content return $content; }
In the code above our function called myFunc will be called every time the content is displayed in a WordPress post. I will be explaining the is more detail later in this post.
With the knowlege of actions and filters behind you, you are ready to write your first plugin.
Setting Up
The plugin we will be creating is a Word Filter plugin. It could be useful on a blog with multiple authors as a swear word filter. We will be replace all of the “naughty words”, which we define with a substitue.
The first thing you want to do is FTP into your Worpdress site and create the plugin folder. The location of the folder should be as follows.
/wp-content/plugins/word_filter
Note that any time you create a plugin, it must go inside of the WordPress plugins folder.
Now create a file of the same name in this folder (word_filter.php). This will be the file that will hold all of our plugin code. Just to reiterate, you should now have a file at this path:
/wp-content/plugins/word_filter/word_filter.php
Now we are ready to begin editing the file…
Adding the Meta Information
In order to identify your plugin (and give you credit for it), you must add the following information to the top of your plugin file.
/*
Plugin Name: Word Filter
Plugin URI: http://brandontreb.com/
Description: This plugin filters out certain words in a post. Could be used as a swear word filter or just a global find and replace.
Version: 1.0
Author: Brandon Trebitowski
Author URI: http://brandontreb.com
*/All of this meta information will be used to identify the plugin. When the user goes to activate it, all of this info will be displayed in their admin panel. It will look something like this:
Coding time…
Writing the Code
The first bit of code for our word filter is the array of words to be replaced. We will be using associative arrays where the key will be the words we are searching for and the values will be their replacements.
// Array of replacements $search_array = array('idiot' => 'nice person', 'shutup' => 'please be quite', 'hate' => 'love');
As you can see, the word ‘idiot’ will be replaced with the words ‘nice person’, etc… You could do any amount of replacement here.
The next step is to write our function that will do the replacing. We will call this function word_filter. One thing you need to be aware of is the function must take exactly one argument. This is because we will be hooking into the the_content filter of WordPress. When our function gets called by the system, it will pass in the post content for every post that gets displayed. Here is what the function will look like.
function word_filter($content) { global $search_array; // Modify the content foreach($search_array as $find => $replace) { $content = str_replace($find,$replace,$content); } // Return the content return $content; }
Pretty simple ehh? We loop over the $search_array and retrieve its keys and values. We then substitue the $find string with the $replace string in the post $content. Finally, we RETURN THE CONTENT. I emphasized that because it is very important. Since you are running this function in a chain of others, you must return the content so that the next function can process it. If you don’t the chain is broken and your posts won’t contain any content.
The last thing that must be done is we must hook into the the_content filter of WordPress. Again, this is how we get our function added to the chain. To do this, simply add the following line.
// Make sure our fn gets called before displaying the content add_filter('the_content','word_filter');
This is just one of the many Filters that you are able to hook in to.
Putting it all together
When reading tutorial, I generally like to see the final source code in once place. So, here it is in all of it’s (very simple) glory.
/* Plugin Name: Word Filter Plugin URI: http://brandontreb.com/ Description: This plugin filters out certain words in a post. Could be used as a swear word filter or just a global find and replace. Version: 1.0 Author: Brandon Trebitowski Author URI: http://brandontreb.com */ // Array of replacements $search_array = array('idiot' => 'nice person', 'shutup' => 'please be quite', 'hate' => 'love'); // Make sure our fn gets called before displaying the content add_filter('the_content','word_filter'); function word_filter($content) { global $search_array; // Modify the content foreach($search_array as $find => $replace) { $content = str_replace($find,$replace,$content); } // Return the content return $content; }
*Note: I omitted the php wrapper tags because they were causing problems in the output. Make sure you add them back in.
There you have it! Your first WordPress plugin. In a later tutorial I will show you how to create an admin panel that will allow users to configure all of the search and replace words.
If you have any comments or questions, feel free to leave them in the comments section of this post.
You may also download the source for this example here.
Happy WPCoding!
Tags: coding, wordpress, wordpress filters, wordpress plugin, wordpress plugin programming
Very nice post! I hope you continue to write about wordpress, it will be very helpful to me!
Great introduction to making plugins – makes getting started quite approachable! Thanks for sharing.
What kinds of custom plugins have you made, just out of curiosity?
@Ray
I have made a ton of internal plugins for work, but I have a couple of public ones on WordPress.org.
http://wordpress.org/extend/plugins/tweetpress/
http://wordpress.org/extend/plugins/twitpop/
Obviously they are both Twitter related plugins. Check them out if you are interested.
Thanks for reading!
Loved the tutorial, thank you
This is the first time I’ve been able to get through one easily and without issues.
Please, please, please, PUH-LEASE continue with this type of thing – I need you now
@Peach,
What would you like to see a tutorial on?
this article give many tips. it is useful.
My mom and I would like to create a blogging site comparable to this for our internet site, I stumbled across your internet site trying to find ideas on the theme as well as layout. I am taking some coding course in college but not sure if I would have the capacity to produce a site such as this one at this time. Did you code this website all by yourself or use a professional?