So I had my first exposure to writing a wordpress plugin yesterday. It was such a pleasant and easy experience that I thought I better give a little back. So here’s my step by step guide to creating a very simple plugin that produces a shortcode replacement plugin. I don’t pretend to be a WordPress expert, so if i’ve buggered up my approach please do comment and let me know.
Create a folder using the name for your plugin, I’ll use demo_shortcode_plugin, within this folder create your main plugin file. So the folder structure should look like this:
demo_shortcode_plugin
– demo_shortcode_plugin.php
– readme.txt (optional)
The first part of demo_shortcode_plugin.php should contain the information required to let wordpress, and wordpress users know what the plugin is, who wrote it and other meta info. Use the example below as a template.
/*
Plugin Name: Demo Shortcode Plugin
Plugin URI:
Description: A simple demo plugin that will replace [democode arg1='test' arg2='test'] with some text
Author: D. Hawkins
Version: 0.1
Author URI: http://www.danhawkins.me.uk
*/
Next write the actual plugin code, below is a very simple example. I’ve opted to keep my bits wrapped up in a lovely class but you don’t need to do this, you can of course you straight forward structured code. The init method will register the shortcode with wordpress so it knows to process it. The init will be called when plugins have loaded using the add_action call at the very bottom of the file.
You can test your plugin by just adding it to wp-content/plugins directory. To upload it all you have to do is zip up the folder and upload.
if(!class_exists("DemoShortcodePlugin")){
class DemoShortcodePlugin{
//constructor
function DemoShortcodePlugin(){
}
//initialise
function init(){
add_shortcode('democode', array($this,'func_democode'));
}
//the shortcode replacement function
function func_democode($attr){
//extract arguments passed in with default values where not present
extract(shortcode_atts(array(
'arg1' => 'Default Arg 1',
'arg2' => 'Default Arg 2'
), $attr));
return "Arg1: {$arg1}, Arg2: {$arg2}";
}
}
}
//create instance of class
$demo_shortcode_plugin = new DemoShortcodePlugin();
//get it to initialise at the correct time, in this case after plugins have loaded in case we rely on other plugins
add_action('plugins_loaded',array($demo_shortcode_plugin,'init'));
The full code is here:
/*
Plugin Name: Demo Shortcode Plugin
Plugin URI:
Description: A simple demo plugin that will replace [democode arg1='test' arg2='test'] with some text
Author: D. Hawkins
Version: 0.1
Author URI: http://www.danhawkins.me.uk
*/
if(!class_exists("DemoShortcodePlugin")){
class DemoShortcodePlugin{
//constructor
function DemoShortcodePlugin(){
}
//initialise
function init(){
add_shortcode('democode', array($this,'func_democode'));
}
//the shortcode replacement function
function func_democode($attr){
//extract arguments passed in with default values where not present
extract(shortcode_atts(array(
'arg1' => 'Default Arg 1',
'arg2' => 'Default Arg 2'
), $attr));
return "Arg1: {$arg1}, Arg2: {$arg2}";
}
}
}
//create instance of class
$demo_shortcode_plugin = new DemoShortcodePlugin();
//get it to initialise at the correct time, in this case after plugins have loaded in case we rely on other plugins
add_action('plugins_loaded',array($demo_shortcode_plugin,'init'));