StackArena | Creating A Simple WordPress Plugin

Thinking of extending the functionality of your WordPress blog? Plugins allow you to do that. Most often than not you’d find a plugin that already does what you are looking for; provided you know how to search.

Piece of cake right? Well writing a plugin is not that hard as well.Some times you just want to do something really really basic or customized for your blog and you realize searching would be a lot more hectic than creating your own.

In this tutorial/tip I’m going to show you how to get started on creating your own WordPress plugin. I assume you are familiar with the WordPress framework and program well in PHP. Here is a basic definition for a WordPress Plugin:

WordPress Plugin

A WordPress Plugin is a program, or a set of one or more functions, written in the PHP scripting language, that adds a specific set of features or services to the WordPress weblog, which can be seamlessly integrated with the weblog using access points and methods provided by the WordPress Plugin Application Program Interface (API). (adapted from wordpress.org)

Creating the plugin

All wordpress plugins are located in wp-content/plugins/. Suppose we wanted to create a plugin to detect browsers and perform different tasks based on the browsers, we could call it “Browser Detector” and call our php file browserdetector.php.

So quickly here are the steps:

  1. Create a folder “Browser Detector” and place it in the plugin directory stated above
  2. Create the php file browserdetector.php in the folder
  3. Write your code in the php file.

 

Step 3 requires a little more than just placing your code. It requires the standard plugin information header. Without this, your plugin cannot be activated and will never run. Here’s the format:

<?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 License: A "Slug" license name e.g. GPL2 */ ?>

Also it is customary to follow the header with information about licensing for the Plugin if you intend to distribute your plugin.

 

<?php /* Copyright YEAR PLUGIN_AUTHOR_NAME (email : PLUGIN AUTHOR EMAIL) This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License, version 2, as published by the Free Software Foundation. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ ?>

 

Now you can place your code in the file. Our sample browserdetector.php code below is a plugin that provides a shortcode that can be placed in a post/page. See Creating A WordPress Shortcode Using A Custom Functions Plugin for more insight.

 

require_once('Browser.php'); function brower_properties( $atts ){ /* * Create instance of a browser */ $browser = new Browser(); $properties = "Platform: {$browser->getPlatform()} <br />"; $properties .= "Browser Name: {$browser->getBrowser()} <br />"; $properties .= "Browser Version: {$browser->getVersion()} <br />"; return $properties; } add_shortcode( 'browserdetector', 'brower_properties' );

 

There you have it! Download resource – the Browser.php class. Visit StackArena and drop questions if you have any trouble.

 

Source StackArena

This article is shared based on the resource partnership between oTeKbits and StackArena. Do get more resource for local developers visit stackarena.com. You can also join the Facebook and Twitter community.

 

Comments are closed.