How to write a WordPress PlugIn?

Did you ever play with a WordPress blog? I bet you must have done it quite a few times if you are interested in creating blogs and making WordPress based CMS websites. WordPress has been among the forefronts in creating a Content Management system to handle blogs. Even more, the polished looks that WordPress offers and the ease of operating it, gives a better outlook towards making a website.
So some among you must be thinking about how do they make those PlugIns? What are PlugIns? Did you want to make PlugIns yourself? While there is a lot of information that comes in learning, here is an article to get a gist about how to write a plug in for a WordPress CMS blog and where to look for further infirmation.

WordPress has been among the top CMSs to create a website. Why? Well, thanks to One Click Install and One Minute Install, Novice also can become a part of “I have a blog” club and boast of being among the tech savvy ones and make an actual step forward to learn the art of website making.
While we speak on Writing a PlugIn, check here if you want to refer to the article on WordPress.

The role of PlugIns come over here. Whether you are a techsavvy person or not, nobody will like to change the basic code that goes in making the website. If anything goes wrong, then your whole site is doomed.

How does code come in between?

WordPress says: “Code is Poetry”. Very nice to hear and soothing to feel. The reality is something different. Code is Code and Poetry is Poetry. Even to that, writing a Poem is a very strange art if you are not into it. So writing poems is a soothing art for the Poets and thus, writing Code for Coders is Poetry!
So, everything in making a website boils down to Code. The header, Navigation, Menu bar, body, sidebars, font size, type, Underline, footer etc etc is seen as set on the page and this setting creates a face of the website. This face will look the same from wherever you look at it; whether you look from the North Pole or even from the South pole of the Earth.
So, as the website keeps on building up, the code expands and the Internet Browsers are doing the job of decoding this code and presenting it in a Human readable format which is nothing but the face of the website.
This code is intertwined and dependent on the other components of the website and very fast enough, this code becomes a myriad of lines of code which can become tedious to handle.
So, to make this more manageable, the Coders started making compartments. Technically, call it as Core and Plug-In.

The Core

The Core is that part of the code which present the basic requirements. So, your website will still run on these basic things. In other words, this code tells the novice that “Do Not Disturb”. Or it tells the savvy people “Proceed with Caution”. Your website can run on this basic code as its barebones and it will not show the awful and dreadful 404 messages!

The Plug-In

Just as its name goes a PlugIn is a patch of code which can be attached to the Core code which now is able to give an enhanced function to the website. This code can be hooked or attached to the core, without making any changes in it and can be

  • Activated and used
  • Deactivated and kept in your folders for future use
  • Deleted without changing the core

.
So now, here is the advantage which can be expressed as if you want to put in a lollipop on to your child’s doll and make her feel happy. So, your child likes the lollipop, plays with it and then as she grows older, you now want to change the lollipop with a candybar. So you just pull out the lollipop and change it with a Candybar. You do not need to change the whole doll.
So you can easily modify, make customizations and enhance your website with these PlugIns.
And WordPress is a Researching and developing organisation which can make changes in the core. These changes do not affect your PlugIns and thus do not stop your website from functioning if the PlugIn stops getting correctly hooked with your website. The PlugIn code will simply not appear if it is non compatible.

The WordPress PlugIn

The WordPress PlugIn is a set of code written in PHP Scripting language. It is also called as a PHP program which has one or more functions which enhances your WordPress CMS based website by adding certain features and is available from the WordPress PlugIn Repository. These PlugIns use Access Points and methods, provided by the Application Program Interface for PlugIns called as PAPI (PlugIn Application Program Interface).
PlugIns if created in a systematic manner work well with the WordPress core installation.

Names, Files and Locations of the WordPress PlugIn

The first step starts with searching for similar PlugIns and to understand how your PlugIn be different from what others have already made and why you should create a PlugIn with your desired functionality. Once you can satisfy yourself, you will be able to satisfy others. So put your thinking cap and start working on the name and try to make it Unique. Names can have a single word in them or can be a few words which describe the purpose of the PlugIn.

Creating the PlugIn file

The first file that you must create should be in PHP and should tell about the PlugIn name. So if the name of the PlugIn is Scrolling Notice Board, then the name of the file could be Scrolling-Notice-Board.php. Keep in mind that the PHP file name must be unique as there can be only one unique file name in the whole WordPress PlugIn repository. One thing to be noticed is that if your PlugIn will have multiple files, then it should be properly arranged in a Directory. A WordPress PlugIn can be made up of 1 single PHP file or may also contain various different files that can have CSS codes or Images and other related files. In such a case, you might also need to use Function References to give information about the file URLs. This helps in the case when the WordPress installation is moved, the PlugIn files can still be dynamically accessed.

The ReadMe File

Although not a required code part of the PlugIn, you must now create the ReadMe file. The ReadMe file forms the description part of the PlugIn. People will be able to know about the PlugIn. The ReadMe file must also mention the “Requires” and “Tested up to” parts in the stable tag. “Requires” mentiones about the version of WordPress installation and other things required for the PlugIn to function properly. “Tested up to” mentions about up to which version of WordPress is the PlugIn tested and found good to work.

The PlugIn home page

Yet another non code part of your WordPress PlugIn, but very much required to describe it. Over here, you must describe your PlugIn in good length. Describe about what it does, how it works, what all things are required and information about WordPress compatibility. You can also give information about what changes and updates were done in each version.

The actual code

After setting up the basic requirements it is now time to write the actual code. WordPress has high requirements and these must be met while writing the code for your PlugIn. If you want your PlugIn to be approved by WordPress and want to place your PlugIn the repository, you have to follow the requirements which are mentioned by WordPress.
The first part starts with the File Headers. These are to be mentioned in the main PHP file. The first part that a Header file can contain is about the Standard PlugIn Information.

What is a PlugIn File Header?

A PlugIn File Header works for the following purposes:

  1. Allows WordPress to know and recognise your PlugIn – that is exists.
  2. The PlugIn can be added to the PlugIn Management Screen in order to Activate, load and execute its functions

If you do not provide such a header information, your PlugIn will not be activated and will never be run, however good your code may be. Thus to have the header, WordPress has given a format which must be as follows. This information is taken from the official WordPress website which can be accessed through the link provided above.

<?php
/**
 * PlugIn Name
 * PlugIn URI
 * Description
 * Version
 * Author
 * Author URI
 * Text Domain
 * Domain Path
 * Network
 * License
 */
PlugIn Name
Mention the name of the PlugIn. The name must be unique and should not be present in any other PlugIn. This must be in normal words and does not need any hyphen in between the words.
PlugIn URI
Mention the link of the website where you have mentioned information about your the PlugIn. This can be the link to the PlugIn description on your Blog or Website.
Description
Give a description to your PlugIn. Describe about its function, about what is does and how it enhances your website. it is a good practice to mention about your PlugIn in detail over here. While it is not a rule, but can be considered as a Thumbrule that the description should be a minimum of 150 to 200 words. Having a original, self written description of 150 to 200 words helps in proper Search Engine Optimization. Do not stuff keywords over here, but write a description in simple English about your PlugIn.
Version
Mention the latest Version number of your PlugIn. The version number can be of the format x.x or x.xx or xx.xx.xx
Author(s)
Mention the name of the Author or the team of Authors involved in the production of your WordPress.
PlugInAuthor(s) URI
Mention the link where information about the Author(s) can be found. This link can be the author(s) description page on your blog or website. Make sure that your Author page is correct, updated and having photographs will be even nicer!
Text Domain
This is a optional mention. Mention the name of the your website or blog domain in plain text. Required if you have translation of your PlugIn. This should be the same as would be mentioned in “load_PlugIn_textdomain()”
Domain Path
This is a optional mention. This is required if you have translations for your PlugIn and if they are located in a directory different from your PlugIn directory.
Network
This is required if the activation of the PlugIn requires to be done over multiple sites on a network. This is particularly useful when you have Multisite feature enabled and being used.
License
This information is for the reader of your PlugIn information. Mention the type of License you have for your PlugIn. If you do not have any copyright license, then mention GPL 2 or later. This mentioned that your PlugIn is in the General Public License category.
License URI
Give a link to the description of the License. This link may be a page on your blog or website or to other pagrs where the official information about the license can be found.

Security Feature – Blocking direct access to PlugIn PHP files

Direct access to your PlugIn file can be done as a security measure. This is not a requirement but can be done if you want to protect your code from being taken by others. This can be attained by mentioning the following code on the top of every PHP file of your PlugIn.

defined('ABSPATH') or die("No script kiddies please!");

Your PHP files and the code must be in UTF-8 format only

The License file for your PlugIn

Make a PHP file and name it as License. A License file is a part of Standard Format for a PlugIn. the text of this file depends on the type of license you want to use for your PlugIn. Most of the PlugIns are under General Public License while some are Copyrighted. Make use of the following format to present your license:

<?php
/* 
Year of Copyright
PlugIn Author Name
PlugIn Author Email
License text
Warranty or Guarantee of PlugIn
Link to License file or the original License file itself
*/

Writing the Program of your PlugIn

After you have completed the basic requirements of your PlugIn, you can start writing the code for the same. This code will decide about how the desired task will be obtained. There are two different ways by which a PlugIn can get their desired work done. These methods are called as Hooks and Tags. There are certain instances when the PlugIn Data needs to be saved in the WordPress Database and also having some administration panels.

WordPress PlugIn Hooks

The meaning of the word Hook tells us itself about how the PlugIn would work. The code is hooked to the WordPress Core by which the Core code executes this PlugIn code at that particular instance. These instances happen as the Browsers read the code and present to the reader. Some of the hooks are filter and action.

WordPress PlugIn Template Tags

Tags are another method by which a PlugIn information can be displayed on the website. These tags are codes which when placed on specific places of the website, can show the PlugIn data.

Saving PlugIn Data in Database

There are certain occasions where such some data needs to be saved in the Database. This database is used for various functions in filter, Action and Template functions. This information can be saved in the WordPress Database by the following methods:

  1. WordPress Option Mechanism
  2. Post Meta Also Known As Custom Fields
  3. Custom Taxonomy
  4. Creating new custom database tables

Administration panels

Once the above steps are accomplished, you will need to create some panels which will enable users to set the functionality of the PlugIns in a way that they can set the PlugIn according to their need, in the limitations of the options available.

Further, you can internationalize the PlugIn. The other word for Internationalizing is localizing. By this way, people from various countries can access your PlugIn in their own language, if available.

Lastly as WordPress gets on updated and new features getting added in the Core, your PlugIn a

lso needs to verify itself and remain compatible with the updated Core. Check the hooks and tags you have applied to your PlugIn and check for international availability. Get to know about what other things and additions need to be done to your PlugIn so that it stays well and works with ease with the websites. Address bugs and make fixes to maintain compatibility with old websites and websites which are on the newest version of the code. Keep in mind that everyone does not have a fast working internet. If you are working on a heavy project, then make sure that you can keep the size of the PlugIn as trim as possible.

So this is the basic outline of how a PlugIn can be written for WordPress and what issues need to be taken care about in the PlugIn. To get information about how to make the hooks and filters, how to add further information about WordPress PlugIn, refer to the official website or get to read our other articles on WordPress by searching or going through this website on How to do things!

%d bloggers like this: