Getting started

Let's assume that you've decided what your plugin should do. Perhaps you have a bit of existing PHP code that you'd like to integrate into WordPress, or maybe you've been playing around with a new function as part of a theme file and you'd like to make it more portable. In any case, you have something in mind for the core of your plugin. Now you need to know how to package it as a plugin, create some options, and perhaps even submit it to the official plugin repository.

The first step is to create your plugin files. While you can create a plugin that's just one file—like the Hello, Dolly plugin included in the WordPress download package—most plugins contain multiple files (even if it's just one PHP file and a readme.txt), so it's a good idea to start by creating a directory for your plugin. It should go inside your /wp-content/plugins directory.

To begin the Next Page plugin, I created a directory and file. The file is saved as /wp-content/plugins/next-page/next-page.php. The first thing that goes into this file is the header comment block, as shown in Listing 9-1. The title and description will appear in your list of plugins. If you provide a URL in addition to your name as the author, your name will be linked to that URL. You may also provide a URL for the plugin, if it has its own page on your website. You should also include a license, either as a single line (as shown) or as a separate comment block (if, for example, you are including the standard GNU Public License header).

Listing 9-1. The Next Page plugin header comment block

Plugin Name: Next Page

Plugin URI: http://sillybean.net/code/wordpress/next-page/

Description: Provides shortcodes and template tags for next/previous navigation in pages.

Version: 1.0

License: GPLv2

Author: Stephanie Leary

Author URI: http://sillybean.net/

There are a few additional requirements for plugins distributed through the official repository. If you intend to submit your plugin, you'll need to include a readme.txt file, some screenshots (optional), and a few extra functions that will allow your plugin to be translated into other languages (also optional, but preferred). These steps are detailed at the end of this chapter.

Once you've created your files, you need to plan your approach to the code itself. You should identify the things that users might want to change and plan to turn those things into options. Then you'll need to create the option page containing the form where users can save their options to the database. You can then go through the parts of your code you identified as potential options and replace them with the variable containing your stored options. To demonstrate, I'll show you the Next Page plugin without its options, then add the options page, and replace the appropriate variables. First, however, I'll show you how to create a very basic template tag.

0 0

Post a comment