WordPress is not just a blogging platform but also a powerful CMS with unlimited capabilities and huge user base.I will show you how to write a Hello World wordpress plugin, which unlike many believe is surprisingly easy, once you understand the very fundamentals.

Plugin Files & Names

Assigning unique names, documenting and organizing the plugin files is very important part of plugin creation.

Hello-world.zip

1)    readme.txt

2)    hello-world.php

Although wordpress allows you to place the plugin php file directly into the wp-content/plugins folder, for a good plugin developer you will need to create a folder named hello-world and place within readme.txt and hello-world.php.The readme.txt contains information about your plugin and can come in handy when you submit your plugin wordpress SVN plugin repository.Go ahead and create these files and we will add the content to these files later.

The Plugin Basics

The heart of a wordpress plugins is the below 2 functions (commonly called `hooks`)
add_action ($tag, $func)

add_filter ($tag,$func)

Plugin Information

Open your hello-world.php and in the first line, add this commented plugin information to your file.

<?php
/*
Plugin Name: Hello-World
Plugin URI: http://yourdomain.com/
Description: A simple hello world wordpress plugin
Version: 1.0
Author: Siri
Author URI: http://siriinnovations.com
License: GPL
*/
?>
Save this php file,
  • Place the plugin folder to wordpress > wp-content > plugins
  • Go to your wordpress admin > plugins and you will see the new plugin listed, waiting to get activated.

But this plugin had to do something right?

Why not we make it print “Hello World” when we call it from wordpress theme template files and for that we write the code using add_action below the commented plugin information in the hello-world.php

<?php
/*
Plugin Name: Hello-World
Plugin URI: http://yourdomain.com/
Description: A simple hello world wordpress plugin
Version: 1.0
Author: Siri
Author URI: http://siriinnovations.com
License: GPL
*/
/* This calls hello_world() function when wordpress initializes.*/
/* Note that the hello_world doesnt have brackets.
add_action('init','hello_world');
function hello_world()
{
echo "Hello World";
}
?>

Thats it! Our Hello World plugin is nearly done and with just few lines of code. When our plugin is activated, add_action command calls our hello_world() function when wordpress starts loading.

Lets Test our Hello World Plugin

We really don’t know whether our plugin works or not. To test our plugin, go to plugins, activate the hello-world plugin.

Then open your wordpress theme wp-content > themes > default, open any of index.php, archive.php or single.php and place the following code anywhere.

<?php
if(function_exists('hello_world')) {
hello_world();
}
?>

The key here is function_exists() call which checks whether the plugin is loaded or not and then allows the hook into the plugin function.Call to hello_world() in the theme files without checking it,often leads to “Fatal error:call to undefined function” and our blog would crash,if the hello world plugin is not activated or deleted.

Select any page and assign the plugin into that page.See that plugin data in that page.

That’s the work of our plugin. It WORKS!