At Agira, Technology Simplified, Innovation Delivered, and Empowering Business is what we are passionate about. We always strive to build solutions that boost your productivity.

,

Guide to Building a Simple WordPress Plugin

  • By Manikandan Thangaraj
  • January 31, 2017
  • 5343 Views

Introduction

In this article, we are going to see how to build a WordPress plugin.

What is WordPress?

WordPress is a free and open-source content management system (CMS) based on PHP and MySQL. We can easily install WordPress on a web server and create our blogging website. It is the most easiest and most powerful blogging and website content management system, not to mention its popularity! WordPress provides more plugins and themes to design your personal website than any other CMS.

So, what is a WordPress plugin?

WordPress plugins are ways to extend or enhance the functionality of a WordPress site. It is these plugins that gives WordPress the power it has, and otherwise, it would be a simple blogging platform. For example, with the Woocommerce plugin we can build e-commerce sites. To put in a nutshell, plugins are used to extend the functionality in our WordPress website.

How do WordPress plugins work?

WordPress plugins work based on a concept called hooks. Hooks (constitute of actions and filters, and) are the core to comprehend so as to work with WordPress plugins. It is with the help of hooks that our customised plugins will run specific functionalities at the times specified. These functionalities are descrived within WordPress functions.

First, let me tell you why i have written this guide.

As mentioned above, In WordPress plugins play the vital role in websites customization. Official WordPress has more than 40,000+ plugins for you to choose, but many of these plugins miss the mark. All the plugins donot meet your requirements or functionality needed by you. Obviously, the solution would be to build our own plugins. And that is why we need to know how to create our own plugins, be it simple or complex or customised.

In this tutorial, you’ll learn how to build a simple WordPress plugin.

CREATE A SIMPLE WORDPRESS PLUGIN

Where do we need to place the Plugin files?

First and foremost, we need a place to hold our plugin files and so we need a folder for this. You can see the folder called “plugins” in your wordpress root folder inside wp-contents directory. This is where your plugin source code should go. The plugin can contain a single file or multiple files. Also, you should create a main file inside this plugin folder with same name as plugin folder. For example, if you are going create a plugin called example-plugin, then the main file name should be created as example-plugin.php.

In order to make the plugin appear in the WordPress admin side:

WordPress has a few pre-defined rules (one of which is annotations). So, the plugin details will be taken from our plugin’s main file. In order for the admin panel’s plugin section to display the created plugin, you can append the lines below in the main PHP file:

<?php
/*
Plugin Name: Example Plugin
Plugin URI: http://example.com
Description: A basic sample plugin that will serve as an introduction
Version: 1.0
Code Author: Mr. Author
Author's URL: http://mrauthor.com
License: GPL2
*/
?>

 

Change the name and URL according to what suits you. Read 7 Most Popular PHP Frameworks For Web Development

As usual, let us begin by saying Hello World from Plugins

The following function will print a hello world when it’s called. First we will prepare the function, then we will make some arrangement to call it as and when we want.

function say_hello($post_ID)     
{
   echo 'This is my first wordpress plugin, and so, let me say Hello World!';
   return $post_ID;
}

 

Calling this function:

Once you are done with your functionality, you should tell WordPress to initialize or call the function. In order to trigger the plugin in WordPress, you have some set of predefined code, which will execute the plugins. These are called hooks (as mentioned earlier in this article). Hooks have a lot of action and filter functions. You can check out all the available hooks in WordPress, here. For our sample plugin, we need to call our function before showing a post content. So, we are going to use it like this:

add_action ( 'the_content', 'say_hello');

 

Here, ‘the_content’ is the hook action function we are setting to execute our functions.

Now you see the plugin in admin panel’s plugin section and you also see the plugin’s status as un-activated. So please activate the plugin so that you can see our message showing at the top of post content on every post.

Now let us see a slightly more substantial example, and for this, let us create a simple Facebook ‘like’ button:

Just saying a message like Hello World doesn’t provide much functionality, right? That is why we shall see this solid yet simple functionality of trying the Facebook ‘like’ button with iFrame.

<?php
function fblike($post_ID)  {
?>
<iframe src="http://www.facebook.com/plugins/like.php?href=<?php echo urlencode('http://'.$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"]); ?>&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light&amp;height=80" allowTransparency="true" style="border:0;  width:4.0px; height:100px;overflow:hidden;"></iframe>
<?php
   return $post_ID;
}
?>

 

Consequently, the above function will show the FB ‘like’ button on the post content page for current page.

add_action ( 'the_content', 'fblike');

 

With this, we are done. Now you can checkout your site, and we will now have a Facebook ‘like’ button on every post page.

Conclusion

Thus, with the above examples, I have just tried to give a quick intro into building a WordPress Plugin. But, actually one can do a lot more with WordPress plugins… for instance, giving particular settings for the admin, triggering plugins on different points, creating your own plugins, etc. In the coming post, I will come up with creating your own WordPress plugin with admin options, interactive functionalities, etc… so as to guide in creating more complex WordPress plugins. Happy coding!

Manikandan Thangaraj

A Passionate full stack developer with 9 years of experience in full stack web application development and expertise in technologies like PHP and Angular. He has huge craze in learning new things about technologies and constantly shares his knowledge with others.