Creating a simple WordPress plugin

pluginWordPress is a great website development tool and it makes it very easy for people with very little skill to create amazing looking websites, knowing very little about website design or coding.

The main appeal of WordPress (in my opinion) is that WordPress using plugins, that allows a standard WordPress installation to be transformed into a website that hardly resembles a standard WordPress site.

So how do we create a WordPress plugin?

There seems to be more than a few different ways to make a plugin. These range from bad spaghetti style coding to elegant pieces of work.

I think my style fits in between the two.

I like to create a PHP class for each plugin, and that way I don't have to worry about function names being used in more than 1 plugin.

In some future posts, I'll show you how to enhance this plugin so that you can access the database, add configuration screen etc.

So here's my basic WordPress plugin structure:


<?php
/*
Plugin Name: The Name of my plugin
Plugin URI:
Description: A decent description of what the plugin does. This is the text that will show up in the plugin page.
Version: 1.0
Author: Bruce Hearder
Author URI: http://brucehearder.com
*/

$b=new bh_simple_plugin('My Simple Plugin', 'My Simple Plugin', '1.0.0', 'bh_my_simple_plugin');

class bh_simple_plugin {

var $siteurl='';
var $options=array();
var $pluginname = '';
var $plugintitle='';
var $version = 'BETA-1';
var $optionname='';

public function __construct($name='', $title='', $optionname='', $version='0.0.0') {

$this->pluginname = $name;
$this->plugintitle=$title;
$this->optionname =$optionname;
$this->version=$version;
$this->siteurl=get_bloginfo('wpurl');
// This hook will fire when the plugin is activated.
// Use it to create database tables, setup initial options etc.
register_activation_hook(__FILE__,array(&amp;$this, 'ActivatePlugin'));

// This hook will fire when the plugin is deactivated.
// Use it to drop database tables (if you choose) etc.
register_deactivation_hook(__FILE__,array(&amp;$this, 'DeactivatePlugin'));

// This will include javascript scripts to be loaded, such as jQuery
add_action('admin_enqueue_scripts', array(&amp;$this,'my_admin_scripts'));

// This will include any text you wish into the <HEAD></HEAD of theadmin page only.
// Note this effect all admin pages, so careful what you do.
add_action('admin_head', array(&amp;$this, 'Load_Admin_CSS'));

// This will setup the admin menu, the items seen on the left hand-side admin menu
add_action('admin_menu', array(&amp;$this, 'ActionAdminMenu'));

//Most of WP is loaded at this stage, and the user is authenticated.
//WP continues to load on the init hook that follows (e.g. widgets), and
//many plugins instantiate themselves on it for all sorts of reasons
// (e.g. they need a user, a taxonomy, etc.).
add_action('init', array(&amp;$this, 'ActionInit'));
}
function __destruct() {
unset($this->options);
}

public function loadoptions() {
$this->options= get_option($this->optionname)?get_option($this->optionname):array();
}

function saveoptions() {
update_option($this->optionname,$this->options);
}

function ActionAdminMenu() {
$handle = str_replace('.php','',basename(__FILE__));

// What access level does the current user have to have before the menu pages are shown.
$permission_level = 'activate_plugins';

add_menu_page($this->pluginname,$this->pluginname,'activate_plugins', $handle, array($this, 'function_1'));
add_submenu_page($handle, 'Menu 1','Menu 1', $permission_level, $handle , array($this, 'function_2') );
add_submenu_page($handle, 'Menu 2','Menu 2', $permission_level, $handle.'_lists' , array($this, 'function_3') );
add_submenu_page($handle, 'Menu 3','Menu 3', $permission_level, $handle.'_subscribers' , array($this, 'function_4') );
add_submenu_page($handle, 'Menu 4','Menu 4', $permission_level, $handle.'_settings' , array($this, 'function_5') );
}

function ActionInit() {

}

function function_1() {
echo '<div class="wrap">
<p>This is the first menu page, put your info here</p>
</div>';
}

function function_2() {
echo '<div class="wrap">
<p>This is the second menu page, put your info here</p>
</div>';
}

function function_3() {
echo '<div class="wrap">
<p>This is the third menu page, put your info here</p>
</div>';
}

function function_4() {
echo '<div class="wrap">
<p>This is the fourth menu page, put your info here</p>
</div>';
}

function ActivatePlugin() {

return true;
}

function DeactivatePlugin(){

return true;
}

function my_admin_scripts() {
// This makes sure jQuery os available for the plugin
wp_enqueue_script('jquery' );
}

function Load_Admin_CSS() {

echo '<style>
/* Put your styles here */
</style>';
}

}
?>

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *