Guide To Creating A WordPress Plugin

Creating a wordpress plugin may seem frightening at first, but once you understand some basic principals and some php, you can create your first (simple) plugin in a matter of minutes!

What you need:
The tools of this trade are simple! You will need:

The Basics of creating a wordpress plugin
 Ok, first thing we need is a basic knowledge of how a plugin is set up.
All plugins consist of at least a php file and a readme.txt file.

The php file should be created following these simple standards:

<?php
/*
Plugin Name: PixelR3AP3R's Functionality Plugin
Description: Function plugin
Version: 0.1
License: GPL
Author: Thomas Volpe
Author  URI: pixelr3ap3r.com
*/
?>

Let’s break it down!

< ?php – starts our php document

Plugin Name – the title of you plugin. This is what users will see when they install your plugin.

Description – describe what the plugin in will do.

Version – what version of the plugin is this?

License – can anyone use this?

Author – your name or company

Author URI – link to your website or more information on the plugin

?> – closes our php document

For more information on choosing your license when creating a wordpress plugin, check out this article 

Now what?
Well, what will your plugin do?

For the purpose of this article, we will be creating a wordpress plugin for Functionality that will allow us to inject our own php functions in to our website without actually using our theme’s function.php file (more on this shortly!)

Let’s get rid of our “Admin Bar” for everyone except the site admin. This is very useful if you want to prevent clients from accessing certain parts of the website (like widgets) through the front-end of the site after you have removed them from the backend.

So our code looks like this:

//remove wp admin bar for everyone except ADMIN
function theR3AP3R_function_admin_bar($content) {
return ( current_user_can("administrator") ) ? $content : false;
}
add_filter( 'show_admin_bar' , 'theR3AP3R_function_admin_bar');

Note: This function was covered in “Remove the WordPress 3.1 Admin Bar” tutorial.

Simply add the above function to our new plugin php file and save it.

DO NOT get lazy and name the file “plugin.php”! This will only frustrate you later if you create many plugins. Be descriptive but not wordy!

I named mine “pixelr3ap3r-functionality-plugin.php”.

Readme.txt

This is where notes, comments, testing/bug issues, changelog and anything else you want to say goes!
Example:

============PixelR3AP3R's Functionality Plugin ============
Requires at least: 3.1
Tested up to: 3.1
Stable tag: 0.1

========================
Description
========================
This plugin will allow you to add php functions to your wordpress site without ever opening your function.php file!

========================
Important Notes
========================
Instal plugin.
To add to it go to
Plugins > Edit > Pixelr3ap3r functionality plugin >pixelr3ap3r-functionality-plugin.php
Add your new functions.
Save!
DONE!!!

========================
Something to remember
========================
If your php is faulty, the plugin will simply deactivate!

========================
Todo
========================

Don't forget this other part too!

========================
Changelog
========================
= 0.1 07/25/2011=
* Added sample base functions to:
    - remove wp 3.1 admin bar
= 0.0 =
* Don't start your plugin at 0.0. That's just dumb.

Save this file as “readme.txt” in the same location as your php file!

All that’s left now is to zip up your “pixelr3ap3r-functionality-plugin.php” and “readme.txt” in to one file called “pixelr3ap3r-functionality-plugin.zip” and install it in to wordpress!

Installation and Use:

Login to you wordpress dashboard
Navigate to Plugins > Add New > Upload
Click “Choose File” and find your zip file
Install

Once activated, you will see “PixelR3AP3R’s Functionality Plugin” in your list of “Installed Plugins”.

Custom Plugin listed

You can click on the “Edit” link under the plugin name to access the “pixelr3ap3r-functionality-plugin.php” file and begin adding your own custom functions!

Why use a plugin?

So why not just edit the “functions.php” file that already came with your theme?

Well, the answer is simple, really.
You will eventually get tired of the look of your website or you client will want to change the look of their site in the future. When you install a new theme, your “functions.php” will be GONE!

So any custom functions you have built up and added to the original file will be lost.

By adding our custom functions to a plugin insures that no matter what happens to the look of our site, our custom php will always come along for the ride!

Anti-FUBAR Security Bonus

By adding our custom php functions to our plugin, we reduce the risk of making our site “FUBAR” since wordpress automatically deactivates faulty plugins. In contrast, if you add bad php to your functions.php file, you can cause massive errors on  your site which can be catastrophic if it is live to the public.

To add or not to add

What should go in our functionality plugin?

Any customizations that MUST always carry over to a new theme.
For example:
– removing the wordpress admin bar for everyone
– custom hooks
– custom taxonimies
– anything that is not theme style dependent

What should not go in to our functionality plugin?

– anything that is directly tied to the current theme
– color, image, margin, padding or any css

The end… for now!

You made it! You have successfully finished creating a wordpress plugin. Now that you know the basics, there is no limit to what you can really do.

If you would like to jump right in to creating a wordpress plugin, you can download my files here, the zip contains everything that was covered here in the tutorial.

Have any questions or comments on creating a wordpress plugin, let me know…

3 thoughts on “Guide To Creating A WordPress Plugin”

Comments are closed.