Monday , 16 May 2016
Home » WP Guides » How to create WordPress Plugin Tutorial

How to create WordPress Plugin Tutorial

Taking the initial steps towards plugin creation

Needless to say, plugins are the easiest solution to most of our needs , which are not already in WordPress. And nowadays, there’s a plugin for everything – from the things that are most necessary to those which are just plain ridiculous. But even the simple fact that they exist is astonishing – it means that WordPress has added a unique way for everyone willing and knowledgeable to expand its functionality. And that can be used to our advantage, if we were to have any particular needs – we can write a plugin for them.

So what exactly is a plugin ? You already know it’s a piece of code that extends the functionality of your WordPress installation , but where is it stored ? And how is it stored ?

WordPress plugin structure

Every WordPress plugin needs to have at least one .php file to exist, in which its information and functionality is stored. However, plugins often need way more than that, they sometimes need additional CSS/JavaScript/XML information. That’s why you need a directory (or “folder” if that’s the term you’re familiar with) that contains all the files of your plugin.

Choose a file name for your plugin that represents it best, and make sure that no other plugin may be using the same need. For example , if your plugin rewrites titles, make its name titlerewr.php

Next off, you need to fill in some metadata – that is information about your plugin which represents it and is used to catalog it. That’s done inside your plugin, on the very first lines of the .php file, as a commentary :

WordPress.org lists this example for a plugin information name :

 

<?php
/*
Plugin Name: Name Of The Plugin
Plugin URI: http://URI_Of_Page_Describing_Plugin_and_Updates
Description: A brief description of the Plugin.
Version: The Plugin's Version Number, e.g.: 1.0
Author: Name Of The Plugin Author
Author URI: http://URI_Of_The_Plugin_Author
License: A "Slug" license name e.g. GPL2
*/
?>

The fields are pretty self-explanatory, and you need to fill them up with information regarding your plugin , if you want it to be legally a good plugin, or if you want to post it to the WordPress.org repository.

The actual programming

Filling up the documentation is easy, but here comes the harder part, even for people who have written a basic PHP script or two. The idea of using your code on another system , means there needs to be a certain way to fetch information from that system, and to place yours in it as well.

That’s why WordPress has its own API (or Application Programming Interface) – this is a sub-system , which allows you to easily interact with the system.

Those interactions mostly rely on several types of mechanisms, which are called “hooks” or “tags”.

Hooks

Let’s first talk about hooks. WordPress is organized in such a manner that it would first search for additional functionalities when performing its basic tasks. So before it generates what you see on the page, it first checks if something is “hooked” to it, and allows it to do what it must, before deploying the information.

Let’s see an example of that. For example ,we run a blog about insurance, and we want to make the word “insurance” display in bold on our site. To make that possible, we’ll write a PHP function to replace “insurance” with “<b> insurance </b>” , which is the HTML code to make it bold. It would look like this:

function make_text_bold($text){
$text = str_replace(‘insurance’,'<b>insurance</b>’,$text)
return $text;
}

If you’ve ever seen even two lines of PHP, you’d know that this function replaces the word insurance with the bolded word insurance, within the parameter string $text , which it then returns. But how will we use that inside WordPress? We need to add a hook, and we can do that with this line:

add_filter(‘the_content’,'make_text_bold’);

This line invokes a function from the WordPress API , which adds a filter (a type of hook) , forcing WordPress to call your function whenever content is displayed.

And with this we have, in essence, created our first plugin! But there’s another very important approach to discuss, and you shall see that below.

Template Tags :

Another way to introduce functionality to your WordPress plugin is if you insert certain function calls inside your themes. For example, if you want something to appear in your sidebar, you can have a function written in your plugin that will be executed inside the sidebar. All you need to do is write that function , and then execute it inside a <?php and ?> pair of “brackets” for example :

<?php your_function(); ?>

There’s a lot more to writing a plugin than what we’ve described here, but thankfully, WordPress provides all you need to know about it’s API in the WordPress Codex. Now you’re free to do some reading and make your first , truly functional plugin! Who knows, it may even be used by thousands of people some day.

 

Leave a Reply