Creating a plugin for WordPress is a really simple process and allows you to create reusable code that you can incorporate into any project or share with others. WordPress has some great support for getting started, I recommend you head over to the Writing a Plugin article in the Codex.

The first step in writing a plugin is creating a directory in wp-content/plugins/ that is the name of your plugin. If your plugin name is “Super Plugin” then name the directory “super-plugin”. Create a file in wp-content/plugins/super-plugin/called super-plugin.php. You’ll need to add some meta information for WordPress to actually allow you to see the plugin in wp-admin on the plugins tab. simply open super-plugin.php and add the following:

<?php
/**
 * Plugin Name: Super Plugin
 * Plugin URI: http://www.webtipblog.com/super-plugin
 * Description: Super Plugin is super cool and will make your WordPress project better
 * Version: 1.0
 * Author: Joe Sexton
 * Author URI: http://www.josephmsexton.com
 * License: GPL2
 */

You now have a plugin, you can activate it in wp-admin in the plugins menu.

Plugin Actions

There are a few handy actions to take note of that will be useful in creating your plugin. register_activation_hook() and register_deactivation_hook() will allow you to trigger a function when the plugin is activated or deactivated. This is great if you need to add a custom database table or remove some data from the database to clean up after yourself when a user removes the plugin.

Pluggable Functions

Creating a plugin allows you to redefine some of WordPress’ pluggable functions which you cannot do from within a theme. I should note that Wordpess is not using this method going forward and is using filters instead but there are still a bunch of pluggable functions that do not incorporate filters.

Shortcodes

Writing a plugin also allows you to create shortcodes that can be added from a theme and the content areas within wp-admin. These shortcodes make it easy for users to include your code in their project. Maybe you want to render a social bug or add a contact form; creating a shortcode can allow a user to simply add that feature to their page. See my article on creating shortcodes using the WordPress shortcode API.