Plugin Development for WordPress

The WordPress platform allows you to modify, customize and enhance your existing website easily. You don’t need to change the core program but rather you can create or modify existing plugins to add the extra functionality that you need. This flexibility in customizing your WordPress installation is provided by the WordPress Plugin API.

There are already hundreds of WordPress Plugins on various WordPress Plugin repositories on the Web where you can download and install. If you cannot find a plugin of your choice, this article will introduce the basics of developing your own plugin for WordPress.

Know the Environment

The first thing you want to do is get familiar with the WordPress environment. This includes how files and folders are organized and arranged and how they are referenced from the WordPress interface.  Since PHP is the language used for Plugin development, having PHP programming experience is a prerequisite. Plugins in WordPress are located inside the folder below:

wp-content/plugins

Create a folder for your plugin. We shall name it “Author”. This is where your plugin PHP and JavaScript code will reside as well as the CSS and images that renders the code to make it user-friendly.

Plugin Header Code

Create a PHP file inside the “Author” folder and name it “author.php”. Add the following code to the file:

<?php
/*
Plugin Name: Author
Plugin URI: http://www.davgit.com
Version: 1.0
Description: A Plugin that displays author info for each post.
Author: David Gitonga
Author UI: http://www.davgit.com
*/

This is standard plugin information header that must be present if WordPress is to recognize the plugin. This plugin info will be displayed when you visit the “Installed Plugins” page.

Adding Functionality

Functions are hard-coded into the plugin files using PHP. In our case, we are going to define one function that calls a section holding the author info. We will also add HTML and WordPress template tags that will be used to build the author info section.

Start by defining the function below:

function author_info()
{
?>

Add the HTML needed to build the page as shown below:

<div class = “author_info”>

<?php

if (function_exists (‘get_avatar’))
{
echo get_avatar(get_author_email(), ‘70’);
}
?>

<?php author_meta (‘description’); ?>

</div>

<?php

Note the template tags used tell you their functionality. For example, the “get_author_email” tag gets the author email from his profile page.

Add the CSS styles that control the HTML page appearance by adding the code below:

<?php
function stylesheet()
{
echo"
<style>
.avatar
{
float:left;background-color: #9A9B9B;padding: 4px;margin: 0 4px 0 0;display: inline;
}
.author_info
{
color: #666;background: #DDDDDD;padding: 8px;margin:0 0 6px 0;
}
</style>
";
}

CSS styles that control the HTML page appearance can also be added by creating a separate file called “styles.css” and then adding the above code to it.

Load the function that will be executed when the plugin loads below:

function display_author_info()
{
if (is_single())
return author_info().stylesheet();
}

Create a file “single.php” and add the code below to it:

<?php
If (function_exists (display_author_info))
Echo
Display_author_info;
?>

We can also add an optional “hook” function that tells WordPress when we want the plugin to be loaded. In our case, we want the author info to be appended between the post content and the comments section. We add the code below to the “author.php” file:

Add_filter (‘comments_template’, ‘display_author_info’);
?>

The plugin is now complete.

NB: Remember to choose a unique name for your plugin folder as well as the main PHP file for the plugin. No two plugins can have the same PHP file name.

If you are hosting your plugin on www.wordpress.org/extend/plugins/, remember to also include a readme.txt file with your plugin using the standard format shown at www.wordpress.org/extend/plugins/about/readme.txt.

Home CSS Deals DesignBombs HTML HTML5 JavaScript jQuery Miscellaneous Mobile MySQL News PHP Resources Security Snippet Tools Tutorial Web Development Web Services WordPress