
WordPress is a very popular development platform for the PHP developers in blog site/simple CMS development. As it became the most popular blogging platform, demand for WordPress plugin development on this platform also increased rationally. There is already resources on WordPress developers center on how to write plugin , but you may find it a little bit descriptive where you really interested in a quick start. Here , i will describe how to start writing a very simple plugin very fast. Our goal from this tutorial is to first make a plugin that will say ‘hello world’ at the beginning of every WordPress posts, then we will enhance this plugin to generate and ‘Facebook Like’ button that will be shown up at the beginning of each post. Of course, I will provide code samples as well to create this plugin on each step for sure. I am assuming here that you have a little knowledge about how WordPress works and familiar with the folder structure.
Where To Place The Plugin And How?
You will see a folder named ‘plugins’ in your ” {root_directory}/wp-contents ” directory. You will have to place your source code on that folder. You can either place it in a single file or use multiple files in a folder. in case of using folder, use the main PHP file name should be same as the folder name. Also, while choosing folder/main file name, please take an unique name so that your plugin doesn’t collide with any other plugin. Lets assume we are using multiple file, so we are gonna create a folder name ‘like-fb’ and then create a file named ‘like-fb.php’ .
What To Do To Appear The Plugin In The Admin Panel’s Plugin Section?
WordPress has some pre-defined rules with which it pares the developed plugin files and retrieve information from them. To get a plugin appear in the admin panel’s plugin section, simply add few lines in the PHP file just like below, of course you will like to place your own plugin name/URL etc instead of mine:
/* Plugin Name: Like FB Plugin URI: http://codesamplez.com Description: A very basic test plugin Version: 1.0 Author: Md. Ali Ahsan Rana Author URI: http://i2dev.com License: GPL2 */
Here every points are self descriptive, so I am not going to tell about these one by one. You can place these info with any order, WordPress will be able to read it, no problem.
Our Hello World Function
The following function will print a hello world message when it’s called. First we will prepare the function, then will make some arrangement to call it according to when we want.
function say_hello($post_ID) { echo 'Hello world. This is my first wordpress plugin'; return $post_ID; }
How To Tell WordPress To Call This Function?
We must have to tell WordPress in some way, when to call the above function. To facilitate this, wordpress provides us a set of code execution points when it will execute our functions. These points are called hooks. We won’t be able to execute any of our plugin functionality besides those hook points, but the existing ones are good enough for our regular needs. For this plugin, we need to call our function before showing a post content. So, we are going to use like this:
add_action ( 'the_content', 'say_hello');
Here, ‘the_content’ is the hook point we are setting to execute our functions. There are lot more, see available hook lists.
Now if you just go to your admin panel’s plugin section, you will see your plugin with un-activated status. Now, please active the plugin. And we are done. Now checkout your site. And what? our message is showing just before the post content on every post, its cool, isn’t it?
Turning This To Facebook Like Button Plugin:
Ok, just saying a message isn’t enough yet. We want to do some solid functionality, though simple. ok, Lets make our function modified a little as follows:
function fb_like($post_ID) { ?> <iframe src="http://www.facebook.com/plugins/like.php?href=<?php echo urlencode('http://'.$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"]); ?>&layout=standard&show_faces=true&width=450&action=like&colorscheme=light&height=80" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:450px; height:80px;" allowTransparency="true"></iframe> <?php return $post_ID; }
As we see, this function actually prints a Facebook like button code. Here, URL is always the current page URL, so that it reflects the current page URL for every post. Now change the hook settings code to the correct function name(here ‘fb_like’):
add_action ( 'the_content', 'fb_like');
We are done. Checkout your site, we now have a Facebook like button on every post page. 😀
You can also download this Like FB Plugin For WordPress to compare if you are having trouble with your one.
Here, by the above php code examples, I have just tried to give you a quick but interesting start. You can do a lot more during wordpress plugin development. Such as, by giving users to set different settings on admin panel, triggers the plugins on many more hook points, etc. Like, to extend the current plugin, you can first try to set some admin options just like you can see on the Facebook like button build page. Then change like button code according to user preference, save their preferences etc, and you will get more interactivity on this plugin too. Happy coding 🙂
Thanks. it’s useful tutorial
Would this have to be a plugin per se? Could you not just add the function to your theme functions . php file?
sure, we could. As I mentioned, this is simple plugin just for demonstration purpose for the tutorial. Real time plugins are with lot more complex functionality.
hi sir, could you share a video regarding wordpress and wordpress plugin in hindi. please.
I’m new to WP. I want to have my users sign in to one of my pages via their email address. Can I do this on my “parent” page and if so, what is the easiest way. Any help is so appreciated.
Thanks –
Thanks! Starting coding =) …
At the time of plugin active i got this message so how can I over come it ?
The plugin generated 1 characters of unexpected output during activation. If you notice “headers already sent” messages, problems with syndication feeds or other issues, try deactivating or removing this plugin.
Really useful and simple plugin tutorials, good job Ali… thank you for sharing.
great job..