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: root\wp-content\plugins\AwesomeWP\start.php
Note that we have created a new directory 'AwesomeWP' for our plugin. Although you can place your plugin file directly under wp-content\plugins\ directory, but making a separate directory helps you managing your files in an elegant way.
Now open wp-content\plugins\AwesomeWP\start.php and write following lines in the file:
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
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:
So we have learnt how to write our first WordPress plugin adn modify our posts through plugin. Happy coding:)
Complete Plugin File:
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: root\wp-content\plugins\AwesomeWP\start.php
Note that we have created a new directory 'AwesomeWP' for our plugin. Although you can place your plugin file directly under wp-content\plugins\ directory, but making a separate directory helps you managing your files in an elegant way.
Now open wp-content\plugins\AwesomeWP\start.php and write following lines in the file:
<?php /* Plugin Name: AwesomeWP Plugin URI: http://webspeaks.in/ Description: My first Awesome wordpress plugin Version: 1.0 Author: Arvind Bhardwaj Author 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
add_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:
add_filter('the_content', 'modifyPost'); function modifyPost($content) { $signature = '<br />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:
<?php /* Plugin Name: AwesomeWP Plugin URI: http://webspeaks.in/ Description: My first Awesome wordpress plugin Version: 1.0 Author: Arvind Bhardwaj Author URI: http://webspeaks.in/ License: GPL */ ?> <?php add_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 = '<br />Have fun with plugins!'; return $content.$signature; } ?>
5 comments
Okay, is that we need to develop plugin in WordPess using Php? I am confused with it. I appreciate for sharing it. Hope you update it..
It’s always nice to see your post with lots of good stuff and nice ideas to enjoy. Thanks for sharing valuable post like this.
SIMRAN
This is my first time i visit here. I found so many entertaining stuff in your blog, I guess I am not the only one having all the leisure here! Keep up the good work.
This is very wonderful so nice and beautiful information thanks for this post.
Wonderful post! I wish I had your insight on this topic and could write as well as you. I hope many people get the opportunity to enjoy this great content.
We would love to hear from you...