
WordPress is a top-rated development platform for 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 are already resources on WordPress developers centred on how to write plugins, but you may find it a little bit descriptive where you are really interested in a quick start. Here, I will describe how to start writing a straightforward 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 for each step. I am assuming here that you have a little knowledge about how WordPress works and are 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 in that folder. You can either place it in a single file or use multiple files in a folder. In the case of using a folder, the main PHP file name should be the same as the folder name. Also, when choosing a folder/main file name, please use a unique name so that your plugin doesn’t collide with any other plugin. Let’s assume we are using multiple files, so we are going to create a folder named ‘like-fb’ and then make 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 that it uses to pare the developed plugin files and retrieve information from them. To get a plugin to appear in the admin panel’s plugin section, add a few lines in the PHP file just like below; of course, you will like to place your plugin name/URL, etc., instead of mine:
/*
Plugin Name: Like FB
Plugin URI: https://codesamplez.com
Description: A very basic test plugin
Version: 1.0
Author: Md. Ali Ahsan Rana
Author URI: http://i2dev.com
License: GPL2
*/
Code language: CSS (css)
Here, every point is self-descriptive, so I am not going to discuss them one by one. You can place this info in 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, we will arrange to call it when we want.
function say_hello($post_ID)  {
echo 'Hello world. This is my first wordpress plugin';
return $post_ID;
}
Code language: PHP (php)
How To Tell WordPress To Call This Function?
We must tell WordPress, in some way, when to call the above function. To facilitate this, WordPress provides us with a set of code execution points for 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');
Code language: JavaScript (javascript)
Here, ‘the_content’ is the hook point we set to execute our functions. There are many more; see the available hook lists.
If you go to your admin panel’s plugin section, you will see your plugin with an unactivated status. Please activate the plugin. And we are done. Now check out your site. And what? Our message is showing just before the post content on every post. It’s 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, Let’s make our function modified a little as follows:
function say_hello($post_ID) {
echo 'Hello world. This is my first wordpress plugin';
return $post_ID;
}
Code language: PHP (php)
As we can see, this function actually prints a Facebook-like button code. Here, the 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');
Code language: JavaScript (javascript)
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 own.
Here, using the above php code examples, I have just tried to give you a quick but exciting start. You can do a lot more during WordPress plugin development, such as giving users different settings on the admin panel, triggering 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 the button code according to user preference, save their preferences, etc. You will get more interactivity on this plugin, too. Happy coding 🙂
Discover more from CodeSamplez.com
Subscribe to get the latest posts sent to your email.
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..