Let’s get you going writing plugins for WordPress. Plugins can be used to
- Add a widget to the front-side, like in a sidebar or footer
- Add or change post content, like adding links at the bottom of a post or hotlinking certain words
- Create custom post types, like for events
- Add meta boxes to the add/edit posts page – with content to help writers
- and much more!
At its heart, a plugin starts with a PHP file. PHP stands for PHP Hypertext Processor. It’s
kinda circular. Each plugin gets its own folder in wp-content/plugins, and the “main” PHP
file is usually the same name as the plugin folder.
Choose a Name
Before we code anything, let’s choose a name for our plugin. If you want to ultimately release your plugin to the public in the wordpress.org repository, or if you at least want to keep that possibility open, it is a good idea to search the repository and see if someone has already named a plugin the same name as yours (and if so, pick a different name.)
You can search plugins here
Make a folder and your “main” PHP file
Create a folder somewhere to hold your plugin, e.g. if our plugin will be called “Plugin Pug” lets drop the space and lowercase it and name it “pluginpug”
Inside that folder create an empty file and name it the same as the folder, but with the extension .php on the end, e.g.
This file will be the “main” PHP file for our plugin. Note: It’s not required that the “main” plugin file be the same name, but it is a convention / best practice in the WordPress community.
Open pluginpug.php in your favorite text editor (I use Sublime) and add these lines at the top. These lines are common to all WordPress plugins.
<?php /* Plugin Name: Plugin Pug Plugin URI: Description: The plugin pug plugin description Author: allendav Version: 0.1 Author URI: http://allendav.com/ License: GPL2 */
The <?php at the start is an instruction to PHP that what is about to follow is PHP code. We need this because PHP files can contain a mix of PHP code and HTML. We'll take advantage
of this later.
The /* indicates the start of a comment block, and the */ indicates the end. A special comment block is used in the "main" plugin file to give WordPress information about the plugin. Edit each of these special comment lines as follows:
Plugin Name : Set to your plugin’s name.
Plugin URI : You can leave it blank for now. Most people will end up putting
the URI (URL) for their plugin in the repository here or sometimes a dedicated web page
for their plugin.
Description : Give a description for your plugin
Author : Give your WordPress.org username, if you have one, or your real name if not
Version : Give it a version. Most “work in progress” plugins have a version
number less than 1. Most plugins use something like x.y or x.y.z where x is a major release,
y is a minor release, and z is a very minor release.
Author URI : Give a URI for yourself, e.g. your blog
License : GPL2. Period 🙂
Save and upload the folder and file to your sandbox (wp-content/plugins). Navigate to wp-admin for your site, and click on Plugins. You should see your plugin in the list!
Click activate on your plugin. Huzzah! Your first plugin is now active!
But it doesn’t do much yet, other than exposing the comment block info. Let’s add some stuff.