Tuesday, April 24, 2012

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.

5 comments:

  1. Nice plugin advice for wordpress. This is really helpful for me.

    Wordpress Development Company

    ReplyDelete
  2. Thanks! It helped me a lot. I was looking for a simple hello world plugin and here you made me simple to understand. Thanks once again!

    ReplyDelete
  3. Thank You very much. It's very much helpful. Will You tell me the hook_name which will place "hello world" in content part.

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

    ReplyDelete

Please feel free to post your comments. If anyone has a good article or good thing to share, just send me that with your name to asadmehmoodstar@gmail.com. and if anyone want so receive updates regarding my blog, he can subscribe to my weekly newsletter on "Subscribe to our mailing list" section.

Thanks