Posted: Feb 22, 2008 - Category: Blogging
Comments: No Comments
Author: Coen Jacobs (Posts)

Writing a plugin for WordPress, it isn’t that hard. The hardest part is coming up with a idea for a plugin that hasn’t been written already. But once you have a idea, that hasn’t been worked out already, the real fun starts.

Writing First WordPress Plugin
Photo by Chaparral [Kendra]

Like many programming or scripting jobs, the creation of a WordPress plugin is all about trial-and-error. You have to try it, to be able to learn how to write a plugin for WordPress. In this tutorial, I’m going to take you trough the basics of writing your very first WordPress plugin.

The plugin that we’re going to create, is going to put an end to the most commonly made spell fault in the world of WordPress. WordPress is spelled like two words, you have Word and you have Press. By putting these two words together, you have the name of the blogging software; WordPress.

What does a WordPress plugin consist of?

A WordPress plugin consists of at least one *.php-file, that executes some code. If it doesn’t do a thing, the plugin will have no use and therefor I don’t consider it as a plugin. Our very first WordPress plugin, will consist out of the minimum of one *.php-file, that will do all the work.

So we’re going to write our first plugin. We’re creating a new file in the folder /wp-content/plugins/ called wordpress-spellcheck.php. This is the *.php-file that forms our plugin. Off course, we are going to open the *.php-file with the start- and end-tag of php; <?php and ?>.

Don’t forget to remove all the spaces and white lines before the start-tag, and after the end-tag. If we don’t, our plugin will produce some errors.

Add some required information in the plugin

Now we’ve created a new *.php-file, we can start writing our plugin. First we need to add some required information to the file. Well, to be honest, the information isn’t exactly required, but it looks nice if you do and it’s not difficult.

First WordPress Plugin Page
The way WordPress will show the information we put in the plugin file

Have you ever seen the information that is shown at the plugin-page of the WordPress administration (links to the author and homepage of the plugin)? That’s the information, we’re going to add. This information can be altered to fit your needs. What you need to fill in, will explain itself;

<?php/*

Plugin Name: WordPress Spell Check
Version: 0.1
Plugin URI: http://webhypes.com/wordpress/plugins/wordpress-spell-check
Description: Replaces all kinds of commonly made WordPress spell faults
Author: Coen Jacobs
Author URI: http://webhypes.com/about/coen-jacobs
*/

This is all the information that we’re giving with the plugin. If we take a look at the overview of plugins installed (or ready to install) of our own WordPress installation, we will notice that WordPress sees the information we’ve put in the file and shows it, just like it shows the information of all the other plugins in the world!

Finally changing the faults to ‘WordPress’

Now that we’ve taken care of the information we want to be shown to anyone who installs this plugin, we can start writing the code, that will actually do what the plugin is meant to do.

We’re going to create a function, that will use str_replace (function of php) to replace everything that we put in a array with the correct way to write “WordPress”. Once the function has finished it’s job, it will return the text of the content. And while we’re writing a plugin, we change the spell faults in the post-titles at the same time.

// Function that will replace commonly used Wordpress
// spell faults to show the name of the blogging
// software correctly.
function wordpress_spell_check($text)
{
	// Fill the array with all the spell faults we would
	// like corrected
	$replace = array(
		"wordpress",
		"word press",
		"Wordpress",
		"Word press",
		"Word Press",
	);

	// Replace the wrong spelled name of the blogging
	// software, with a correct spelled name; WordPress
	$text = str_replace($replace, 'WordPress', $text);

	return $text;
}

WordPress needs to know about our function

Now that we’ve created our function, all we need to do is tell WordPress what it needs to do with this function. WordPress uses functions to filter the text, one of those functions is exactly what we’re going to use. We are going to use the add_filter function twice, once for fixing the spell faults in the content and the second time for fixing the spell faults in the title.

// Apply the filters, to get things going
add_filter('the_content', 'wordpress_spell_check');		// content
add_filter('the_title', 'wordpress_spell_check');		// title	

?>

This is the last thing we will need to add, in order to make the plugin work. These two function, or actually this single function that we use twice, will put the text of the content and the title, through our function. In the function, the spell faults will be replaced with “WordPress”.

Things to keep in mind

Off course, there is a lot to improve on this plugin, but why should we? Creating our very first plugin was great fun and really wasn’t that hard. We didn’t want to create a plugin, that we could use. We wanted to create a plugin, because we want to learn how to do that. There’s just a couple of things you should keep in mind, before actually using this plugin.

The function we’ve created will replace all the words or combinations that are stored in the $replace-array. If we use one of these words or combinations in paths to images or something, the images will no longer function, since this plugin will alter the path of the image too.

You can download the plugin, in our plugin section. Feel free to explore the world of plugins and use my code to expand your knowledge. Have fun!

Share this article
Enjoyed this article? Share it with your friends via one of the social networks available. You want to read more of our articles? Feel free to subscribe to the Webhypes RSS-feed.

Digg  Sphinn  StumbleUpon  del.icio.us  Reddit  Technorati  Google  Facebook  Fleck  TwitThis 

Comments »

No comments yet.

Name (required)
E-mail (required - never shown publicly)
URI
Your Comment (smaller size | larger size)
You may use <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong> in your comment.
This could be your advertisement
Clicky Web Analytics
This could be your advertisement
This could be your advertisement