WordPress For the Web Developer: How To Write Your Own Plugins
Here's the high-level overview of the process:
- Select a unique name for the plugin and main function
- Create directory, main and readme files with header and license information
- Write and debug the code (easy, huh? :-)
- Zip up the code directory, and install plugin on WordPress for final testing
Name That PluginAll WordPress plugins must have a name, but more importantly, the name must be unique across all WordPress plugins, to prevent the wrong code from being executed. So before you get too involved in writing code, take a few minutes to search the currently available plugins and pick a good, descriptive name that nobody else is using.
To stay compatible with the main WordPress system, plugins are coded in PHP (if you're not up to speed on PHP, please check out our excellent tutorial series here). Like the name of the plugin itself, the main PHP file containing the plugin code should also be named descriptively, as well as not matching the name of any other WordPress plugin file.
If you're writing a very simple plugin, or one you don't plan to release to anyone else, this single PHP file may be all you'll need in the end, and like any other plugins, it will be installed into the wp-content/plugins/ folder. However, in most cases you'll be better off setting up a directory structure. That way you can have something like this under wp-content/plugins/:
myplugin/ myplugin/myplugin.php (mandatory; the main plugin code) myplugin/readme.txt myplugin/screenshot.png myplugin/images/ myplugin/css/ myplugin/html/ myplugin/js/All the extra files and subdirectories are optional, but if you need them, it's much more clear and professional to keep them organized in a way similar to this. It doesn't create any additional work or problem for the eventual user, because plugins are commonly distributed as ZIP files which preserve the directory structure. In order to function with the WordPress plugin system, your plugin must also have its name declared in a header at the very beginning of the file. Assume this is myplugin.php:
<?php /* Plugin Name: My Plugin */ ?>This may not do much (and in fact, does nothing), but it is a legal plugin and if properly installed in the plugin directory, it will appear on the Manage Plugins page. Normally, though, you'd want to add some more header information, using this format:
Plugin URI: http://PluginHomePage.com Description: Plugin description in a few words or more Version: Version number such as 2.3 Author: Your Name (if you wrote it) Author URI: http://YourHomePage.com License: GPL (or whatever license terms you choose)Just to demonstrate the basic structure, let's write a complete simple plugin, called catify.php. By now, you should be able to copy/paste this into a file, install, activate, and try it on your own.
By using the syntax
add_filter('select_text', 'function')we're telling the plugin to make the named function operate on the text we've selected. In this case, the_content refers to the entire post content.
At this point you'll have to refer to the WordPress API documentation to go much further. This is a link to the general plugin documentation, and here is a list of plugin resources. The WordPress Plugin API page is relatively easy to find, but the lists of action and filter references are not as obvious. All this is a good start; the next article will help you learn how to make more functional plugins.