HowTo – WordPress – Writting Plugins Getting Started

Filed under: Wordpress Tips

wordpress-logoThere are times when you want to add new features to your website and WordPress provides a number of ways to do this without editing the core code. It is important that you limit editing of core code files to a last resort. The reason being that any future update of WordPress will wipe out all of your work and this may make you reluctant to upgrade even when there are security or functional improvements.

Adding Without Changing
There are a number of ways that you can add or alter how WordPress works:

You can
Edit your Theme files, inserting code or making calls to outside files.
Add code to a functions.php file and make calls to it within your theme.
Make calls to internal hooks in the core code and override those functions using your functions.php file.

And finally you can develop a plugin.

Why Write A Plugin?
The choice to develop a plugin rather then editing your own files comes when you find that your solution is one that may be needed by other WordPress users.

By writing a distributable plugin users can upload your code to the Plugins directory, Make any settings needed including adding a widget to their sidebar and then get back to adding posts to their site.

You need to remember that 80% of the people using WordPress never even make basic html changes to their Themes let alone take the time to learn php, sql and everything else needed to add function to their site. If you have some code that people could enjoy then sharing it means the WordPress project will continue to attract users and core development will continue. That way you don’t need to go out and write your own CMS Bloging software from scratch.

So, join in and share your code!

Getting Started

Naming
With over 4000 plugins and more every day Unique Names MUST be used throughout your plugin.The names of your Plugin Directory, Files, Functions and so on should use names related to your specific plugin. An easy way to do this is use an abbreviation of your PUBLIC plugin name as prefixes.

If you are making a calendar plugin named Road Trip Calendar then do something like:

Plugin Name: Road Trip Calendar
Directory:
/plugins/rtcalendar
Plugin file: /plugins/rtcalendar/rtcalendar.php
Function: function rtc_date_thingy {
Options: get_option(‘rtc_username’)

This is good practice in PHP it is also very important when your code has to function along with thousands of other peoples code. Inevitably you are going to have conflicts and people will notify you about your plugin not working when another is loaded and this should help you track down where you need to make changes.

Your Plugin Header

In order for WordPress to identify your plugin you need to add some information to the top of your main php file. The information will show up in the Plugin Add and manage page and be placed into the appropriate columns so your users can read it. This information must be at the top of your file.

Your plugin License info should be placed just below this identifying information to let people know any restrictions on the use of your plugin and to give you and others credit for the code.

.

<?php
/*
Plugin Name
: Name Of The Plugin
Plugin URI:
http://URI_Of_Page_Describing_Plugin_and_Updates
Description: A brief description of the Plugin.
Version:
The Plugin’s Version Number, e.g.: 1.0
Author: Name Of The Plugin Author
Author URI: http://URI_Of_The_Plugin_Author
*/

/*  Copyright 2009, Zippy Moosalot (email : zippy@themoofarm.com)
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License
*/

THE REST OF YOUR CODE GOES HERE
?>

.

Your ReadMe.txt file
You should include a readme.txt file with your plugin so people can get information about your plugin,  install notes, updates and any other information that might be useful to your users.

Adding help information here may reduce headaches down the road due to lost users.

Also do the world a favor we all know Pico is the best text editor out there but use CR and LF in your ascii text files it is a real drag having to go through files converting them. YOU KNOW WHO YOU ARE. :o)

Before We Move to the Code

This is the bare minimum that you need to do before you start coding your plugin.

Proper setup of your files, locations and naming will let your plugin work nicely with other people’s sites. Remember that once you decide to release your plugin to the world you can no longer depend on how it will interact with other peoples code modifications.

Always take precautions and use prefixes and unique names even for your CSS formatting you should have unique Classes and IDs  that won’t be confused with other formatting.