WordPress plugins provide added functionality to your WordPress websites or blogs. These plugins can be a simple single PHP file plugin, or they can be something as complicated as a full-fledged eCommerce plugin. What is more important is to get a good understanding of concepts required to create any kind of plugin. Fortunately, the base concepts are simple, and can be extended to create any kind of plugin that you can possibly imagine.
In this tutorial, we will try to understand the base concepts required to create the “Hello World!” plugin that prints the message “Hello World!” on the main page of your WordPress website or blog.
The first thing to understand is the folder structure, and the files required.
Ideally, the folder and the main plugin file names should be something similar to that of the plugin you want to create, and they should be unique. For the ‘Hello World!’ plugin, we will use a single file hello_world.php, and place it under the folder helloworld in the plugins directory. The plugins directory is in the wp-content directory of your WordPress installation.
So the structure will look something like the following:
+wp-content
-plugins
– helloworld
– hello_world.php
For a more complex plugin the file-folder structure can include sub-folders and other files. For example other PHP files, and sub-directories for images, JavaScript, style sheets and others. The file-folder structure can mimic the file-folder structure of a full-fledged website.
Now, with the help of your FTP program, if your site is hosted remotely, go to thewp-content directory, and create a folder helloworld inside it.
Next, open a text editor, and create an empty file hello_world.php, and place it in the helloworld directory.
If you navigate to the Installed Plugins page of your WordPress administration section at this time, you will still not be able to see the “Hello World!” plugin listed there. For the plugin to be visible, open the file hello_world.php in the text editor, and add the following lines at the top of the page:
<?php
/*
Plugin Name: Hello World
Plugin URI: http://www.yourdomain.com
Description: Description of the plugin that will be available in the admin section of the WordPress under Installed Plugins section
Version: 1
Author: Your name here
Author URI: http://www.yourdomain.com
*/
The above code creates the information shown on the Installed Plugins page of your WordPress administration section. Plugin URI should point to the site where you will provide plugin updates and installation details. Other information can be your own information.
Optionally, after the above information you can add the license information for your plugin. Examples of licensing information are available on thecodex.wordpress.com website, or you can use your own licensing information.
Now when you go to the Installed Plugins section of your WordPress administrator section, you plugin will be visible. If you activate the plugin now, it will not do anything, as we have not added any function to it yet.
Next in your hello_world.php file, add the following function:
function helloWorld(){
print “Hello World!”;
}
This function does nothing apart from printing “Hello World!” when invoked.
Your hello_world.php file will now look something like follows:
<?php
/*
Plugin Name: Hello World
Plugin URI: http://www.yourdomain.com
Description: Description of the plugin that will be available in the admin section of the WordPress under Installed Plugins section
Version: 1
Author: Your name here
Author URI: http://www.yourdomain.com
Licensing information can go here.
*/
function helloWorld(){
print “Hello World!”;
}
?>
Still if you activate the “Hello World!” plugin it will do nothing. Now to print “Hello World!” on the main page of the WordPress website or blog, include the following code at the bottom of the index.php file of your current WordPress theme.
<?php
if(function_exists(‘helloWorld’)) //just checks if the function helloWorld() exists
{
helloWorld();//Call helloWorld() function, which will print “Hello World!”
}
?>
Now save the changes to the index.php file, and activate the plugin. When you refresh the main page of your WordPress blog or website, you will now see “Hello World!” printed at the bottom of the page.
When you create a fully functional plugin, you may need to include other files like readme.txt, install.txt that will provide additional information about your plugin.
For more details,and further information, refer to numerous examples available on the internet, and visit codex.wordpress.com and similar sites for WordPress plugin development best practices.