Create your very own WordPress plugin

A request that I’ve received more than once lately, is to make a rewrite on my ‘How to create a WordPress plugin’ that I published on one of my previous blogs. Off course, I’m more than happy to publish it here again, mostly because it is so easy to create your own plugins and you can start right now!

The official documentation on Writing a WordPress Plugin is great, but it lacks in a usable, clear example of how to write a plugin for WordPress.

All plugins have some required comment fields at the very top of the file. These comment fields give all the information a WordPress blog needs to identify and start using your very own plugin. Let’s take a look at it;

<?php
/*
Plugin Name: Plugin name
Plugin URI: URL to the plugin page
Description: A description of what the plugin does
Version: Version number
Author: Your name, as the author of this plugin
Author URI: URL to your homepage
*/
?>

Now WordPress knows what our plugin is about, let’s add some functionality to it. In my previous example, the plugin was used to correct frequent misspellings to the name of our favorite blogging tool, let’s do that again.

Let’s build your first WordPress plugin

All plugins live (by default) in the /wp-content/plugins/ directory. Create a file called my-first-plugin.php there and paste the comments we’ve just discussed into the file.

<?php
/*
Plugin Name: My very first WordPress plugin
Plugin URI: http://coenjacobs.net/blog/create-wordpress-plugin/
Description: Corrects frequent misspellings in the word: WordPress
Version: 0.1
Author: Coen Jacobs
Author URI: http://coenjacobs.net
*/
?>

As you can see, I’ve added some real values to the comment fields. You can edit them and make it your own plugin by adding your own name and URL’s.

The function that we will use to correct these frequent misspellings is quite simple and uses the PHP function str_replace() to replace certain parts of a WordPress posts content. We will also use a array to select which misspellings we will correct with this plugin to make it really easy to add new frequent misspellings.

Let’s take a closer look at this function;

function correct_misspellings($text)
{
 $misspellings = array(
 "wordpress",
 "Wordpress",
 "wordPress",
 );

 $text = str_replace($misspellings, 'WordPress', $text);

 return $text;
}

That’s basically it. All this function does is take the provided variable $text, and have all occurrences of a string inside the array called $misspellings be replaced with the string ‘WordPress’. After that it returns the variable $text.

Hook up our function!

But how does WordPress know how and when to use this function? With WordPress hooks, we can tell WordPress when to use functions from our own plugins. The hook that we will use for this plugin is the_content, the hook that is used by WordPress to show the content of a post. When we run our function on that hook, it will be applied to all posts and that’s exactly what we want. Let’s hook this function up!

add_filter('the_content', 'correct_misspellings');

This little line of code is all we need to make sure our plugins function will be used inside the the_content hook. Using a WordPress function add_filter() we add a new filter to the the_content hook that is provided as the second argument.

Now, the code of our very first plugin is complete;

<?php
/*
Plugin Name: My very first WordPress plugin
Plugin URI: http://coenjacobs.net/blog/create-wordpress-plugin/
Description: Corrects frequent misspellings in the word: WordPress
Version: 0.1
Author: Coen Jacobs
Author URI: http://coenjacobs.net
*/

function correct_misspellings($text)
{
 $misspellings = array(
 "wordpress",
 "Wordpress",
 "wordPress",
 );

 $text = str_replace($misspellings, 'WordPress', $text);

 return $text;
}

add_filter('the_content', 'correct_misspellings');

?>

When we save all this code in our my-first-plugin.php file and activate it through the WordPress backend, our plugin will start working immediately and start fixing those irritating misspellings in the name of our favorite blogging tool!

Add some extra misspellings to the plugin

Because we use a array filled with misspellings, we can easily add a new misspelling to be corrected by our plugin, just by adding a new string to the array. When we add a new line of one of these highlighted lines, we can add a new string that contains another misspelling and it will be corrected right away;

function correct_misspellings($text)
{
 $misspellings = array(
 "wordpress",
 "Wordpress",
 "wordPress",
 );

 $text = str_replace($misspellings, 'WordPress', $text);

 return $text;
}

Disclaimer

Off course, this plugin isn’t really usable since it replaces all misspellings everywhere throughout the site, even at certain places where you don’t want it to. But it shows that creating a plugin isn’t that hard and can be done in just a matter of minutes.

Share this post on social networks:

  • Twitter
  • Facebook
  • Digg
  • Sphinn
  • StumbleUpon
  • del.icio.us
  • Technorati

Related posts:

  1. Using conditional WordPress plugin file loading
  2. Create wireframes to build WordPress themes faster
  3. WordPress plugins: Use conditional file loading
This entry was posted in Archived. Bookmark the permalink.

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>