Creating a simple WordPress plugin

This is an introduction to writing plugins for WordPress. For this tutorial you will require: a test WordPress installation (don’t try stuff like this on your live server!) with access to the ‘wordpress/wp-content/plugins’ directory and some knowledge of PHP or programming in general.

WordPress plugins are created using a language called PHP. There are plenty of tutorials out there about PHP. I’m not going to cover that in this tutorial.

Create a directory called ‘a-simple-plugin’ in the ‘plugins’ directory of your WordPress install (the usual directory structure is ‘wordpress/wp-content/plugins’). In this directory create a file called ‘a-simple-plugin.php’ and paste in this code:


<?php
/*
Plugin Name: A Simple Plugin
Plugin URI: /
Description: A little tutorial on writing a plugin for WordPress
Version: 0.1
Author: James Wilkes
Author URI: /
License: GPL2
*/
?>

This doesn’t do anything but will appear in the plugins list and can be activated.

Now try this next code. It does 2 things – it adds a widget to your admin dashboard page saying “hello dashboard” and it adds the text “hello world” to the start of every post. Make sure you know how to deactivate the plugin before trying it. If the worst happens you can delete the plugin directory completely and it’ll go away.


<?php
/*
Plugin Name: A Simple Plugin
Plugin URI: /
Description: A little tutorial on writing a plugin for WordPress
Version: 0.2
Author: James Wilkes
Author URI: /
License: GPL2
*/

/******************
* dashboard stuff *
******************/
function a_simple_plugin_dashboard_widget_function() {
echo “Hello Dashboard!”;
}

function a_simple_plugin_add_dashboard_widgets() {
wp_add_dashboard_widget(
“a_simple_plugin_dashboard_widget”,
“‘A Simple Plugin’ Dashboard Widget”,
“a_simple_plugin_dashboard_widget_function”);
}

add_action(‘wp_dashboard_setup’, ‘a_simple_plugin_add_dashboard_widgets’ );

/**************************
* post and comment filter *
**************************/
function a_simple_plugin_filter ($content) {
return “Hello World!<br />\n” . $content;
}

add_filter(‘the_content’, ‘a_simple_plugin_filter’);

?>

Now deactivate the plugin and the messages will disappear.

You could use code similar to this to filter content. I’ll be expanding on this in my next article with a simple, not very effective security plugin to act as an alarm for your blog having been hacked in some way and disables anything that looks like hacked malicious content.

Share this:
Share this page via Email Share this page via Stumble Upon Share this page via Digg this Share this page via Facebook Share this page via Twitter

Leave a Comment