Tuesday 20 May 2014

How to develop a "Hello World" plugin in wordpress

How to develop a "Hello World" plugin in wordpress

Hi all, hope you all are doing well and enjoying best of your healths.

Today i had been searching word-press official website for any tutorial for "how to develop a hello world plugin" and i couldn't find any good tutorial for this. Then i studied some other tutorials and asked my friends about it and thought that why not i should post this on my blog with very simple words, so that others can find a good tutorial on this. So here we go for the tutorial....

Step 1

Create a directory with name “hello-world-plugin” under wp-content/plugins, then create a file “hello-world-plugin.php”. put the following code to this file and save it.
1:  <?php  
2:  /*  
3:  Plugin Name: Hello world plugin  
4:  Plugin URI: http://localhost/wordpress/  
5:  Description: Add HTML snippets to posts and/or page footer.  
6:  Version: 0.1  
7:  Author: me  
8:  Disclaimer: No warranty, use this in your own risk.  
9:  */  
10:  ?>  

You have successfully created your first "Hello world" plugin and you can now activate this like other plugins from word-press admin panel. But for now this plugin wont do anything because we haven't put any logic behind this. so lets add some working code.

Step 2

for now “hello-world-plugin.php” file contains just plugin info. Let us open this and put below code under plugin info and save.
1:  add_action('wp_footer', 'hello_world_plugin_wp_footer');  
2:  function hello_world_plugin_wp_footer(){  
3:       echo '<div>Hello world! '. get_option('blogname') .'</div>';  
4:  }  


after adding this, just refresh your page and see "Hello World" printed in footer of your page. But make sure you have activated the plugin from admin panel.

In above code you can see add_action() function. Actually this is called action. Word-press follows hook architecture and this is the way to bind our function with word-press hooks. Word-press hooks is another topic and you can read this topic here.

That is all and we have now developed very simple "hello world" plugin in word-press. The whole code looks something like this,


1:  <?php  
2:  /*  
3:  Plugin Name: Hello world plugin  
4:  Plugin URI: http://localhost/wordpress/  
5:  Description: Add HTML snippets to posts and/or page footer.  
6:  Version: 0.1  
7:  Author: me  
8:  Disclaimer: No warranty, use this in your own risk.  
9:  */
10:  add_action('wp_footer', 'hello_world_plugin_wp_footer');  
11:  function hello_world_plugin_wp_footer(){  
12:       echo '<div>Hello world! '. get_option('blogname') .'</div>';  
13:  }  
14:  ?>  

Although this is a very simple plugin, but this teaches you how to take initiative for developing a plugin in word-press.

Please feel free to post your comments and feedback.

No comments:

Post a Comment

Featured post

Life Infotech now a leading brand in the field of technology training

  Life Infotech now a leading brand in the field of technology training & its invites students around the nation to be a part of the Tra...