Introduction to writing WordPress plugins

WordPress has an extremely easy to use plugin system which gives developers huge flexibility when it comes to building a web site. In this tutorial I will cover the basics of writing your very own WordPress plugin.

So to start with we need to create the plugin file. All plugins are stored in wp-content – plugins, so in this directory create a folder called test and a file within it called test.php. For WordPress to identify our plugin and know what its called and so on we need to write a few comments. Note* any white space before or after our php block will result in “headers already sent” errors.

/*
Plugin name: Test plugin
*/

Now if we go to the plugins control panel in the WordPress administration area you will see this.

So we now have our plugin however there is no description, version number or author info. To add all of this information to the plugin we simply add more to our comment.

/*
Plugin name: Test plugin
Plugin Description: This is a test plugin
Version: 0.00001
Author: Your Name
Author URI: http://www.domain.com
*/

Now we can really get started with developing our plugin. First of all activate the plugin by clicking “activate” to the right of the plugin information. If we need an actual administration area for our plugin then we will need to add our plugin to the navigation, This is extremely easy to do.

function test_nav() {
  //add_options_page(Title,Menu title,Access Level,File,Function)
	add_options_page("test","test",1,"test","test_admin");
}
add_action("admin_menu","test_nav");

Here we have created a function called “test_nav” we have then used the function “add_action” to tell wordpress what to do with “test_nav” and then in “test_nav” we are simply passing data to wordpress to add to the settings menu. The last parameter of “add_options_page” is the function that will be called when ever some one follows the link from the settings menu so we need to create a function “test_admin“.

function test_nav() {
  //add_options_page(Title,Menu title,Access Level,File,Function)
	add_options_page("test","test",1,"test","test_admin");
}

add_action("admin_menu","test_nav");

function test_admin() {
	echo "Hello world";
}

If you now click the test link in the settings menu you should see “Hello world” on your screen.

The way WordPress allows you to manipulate and add data to the front end of the blog is by writing functions and then adding the function as a filter to WordPress.

function replace( $content ) {
	$content = str_replace("WordPress","<a href='http://www.wordpress.org'>WordPress</a>", $content);
	return $content;
}

This is a very straight forward function, its simply taking what ever data is in “$content” and replacing the word WordPress with a link to the main WordPress site. Now to add this function as a filter to WordPress we just call the function “add_filter

add_filter("the_content","replace");

The first parameter is the WordPress function which we want to add the filter too and the second parameter is our function (which is the filter).

Here are two links to the documentation on the WordPress web site where you will find more information about the WordPress functions and template tags.