WP plugin from scratch


Recently I started a project in WordPress in which I needed to create few plugins. I had no idea where to start from. But searching the web for few minutes gave me plenty of information to boost me up. So here I will show you how to create a wordpress plugin from scratch.

First of all you must decide a unique name for your plugin beacause WordPress’s plugin directory contains a number of plugins and your name may conflict with that of others. So find a unique name:)

Lets name our plugin ‘AwesomeWP’. 
Here is the directory structure for our plugin: rootwp-contentpluginsAwesomeWPstart.php
Note that we have created a new directory ‘AwesomeWP’ for our plugin. Although you can place your plugin file directly under wp-contentplugins directory, but making a separate directory helps you managing your files in an elegant way.
Now open wp-contentpluginsAwesomeWPstart.php and write following lines in the file:

?

1234567891011<?php/*Plugin Name: AwesomeWPPlugin URI: http://webspeaks.in/Description: My first Awesome wordpress pluginVersion: 1.0Author: Arvind BhardwajAuthor URI: http://webspeaks.in/License: GPL*/?>


Go to wordpress admin > plugins and you will see the AwesomeWP listed there. Happy to see it? Hmmmm! Its the comment part in the header portion of file that helps WordPress finding you plugin.
Now lets add some functionality to our plugin as till now it does nothing. Add following lines to our start.php
?

12345add_action('init','awesome');function awesome(){ echo "WordPress is just Awesome!!";}

The ‘init’ is called when WordPress initializes. So we have called a function ‘awesome’ on initialization of our blog. Note that no braces are required during this function call. After activating the plugin you will see ‘WordPress is just Awesome!!’ printed on top of your page, means it works!

Modifying the Post:
WordPress provides us very powerful means to modify literally any part of the wordpress blog. Like we can change the outcome of the posts as to our desire by just adding following lines of code in our plugin file:
?

12345678add_filter('the_content', 'modifyPost'); function modifyPost($content){$signature = 'Have fun with plugins!';return $content.$signature;}

What actually happened here is that we applied a WordPress filter ‘the_content’ in our plugin. This hook is called when post is diplayed on frontend. So at the time of displaying post, we added our custom text below the post. Note that the content of our post is automatically made available in the callback function ‘modifyPost’ through the wordpress filter. This is the beauty of WordPress.
So we have learnt how to write our first WordPress plugin adn modify our posts through plugin. Happy coding:)

Complete Plugin File:
?

12345678910111213141516171819202122232425262728<?php/*Plugin Name: AwesomeWPPlugin URI: http://webspeaks.in/Description: My first Awesome wordpress pluginVersion: 1.0Author: Arvind BhardwajAuthor URI: http://webspeaks.in/License: GPL*/?><?phpadd_action('init','awesome');/*Called when WordPress Initializes*/function awesome(){ echo "WordPress is just Awesome!!";} add_filter('the_content', 'modifyPost');  /*This tells WordPress that every time it's going to display the content of a post or page, it            should run it through our modifyPost() function.*/function modifyPost($content){$signature = 'Have fun with plugins!';return $content.$signature;} ?>

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s