Webbing Wednesdays Week #6

Building WordPress Plugin

Introduction

Plugins are the heart of WordPress. It enables the website to create the features or modify them without changing the core files of the WordPress. It is advisable to create a plugin to enhance the WordPress features and take maximum advantage of this amazing leverage WordPress offers.

Here is a basic definition of WordPress Plugin:

A Plugin is a set of functions written in PHP to add specific feature(s) or service(s) to the website. A Plugin is seamlessly integrated with WordPress and its application interface offers access points and methods to access the same. Are you looking to enhance your WordPress website or a blog to have certain feature as a requirement? Well, WordPress Plugin repository is the first thing one should check before building up the plugin. If close match is found it is advisable to modify that plugin to match the requirements, however if not, just follow the guidelines of this article which will certainly offer control over building the WordPress Plugin. In order to explain how WordPress Plugin should be built, it is imperative to understand the folder structure of WordPress.

WP Folder Structure and Where to create Plugin

WordPress has many folders like wp-admin, wp-content etc. wp-content folder has a subdirectory called plugins. This is where the plugin should reside. The plugins of less complexity and the ones, which requires only one file, can remain in that folder. However to build more complicated plugins, it is important to create a folder named after your plugin name. This folder can contain CSS, JS and HTML along with the PHP functions and coding structure that has to be integrated. Not only this, if this plugin is planned to be made available to developers, it will be a very good practice to attach a read-me.txt file in order for developers to understand programming structure and the definitions of the functions. The file should include the author name, along with the revision details, URL, Name of the plugin and the licensing terms. The question arises on how WordPress understands that a new plugin has been created?

How to Define a Plugin

Any developer needs to define the plugin for WordPress to recodnize it and allow it to be installed, removed and activated. In order to achieve this, the main file should contain the following details:

The above code is highly self-explanatory. One should replace each line of the code to the correct and relevant details. WordPress however only understand the Name of the Plugin. Once that is defined WordPress will definitely consider the file as a plugin.

WordPress Plugin Hooks

Is it necessary to create all the functionalities of the plugin? The answer is No, you can certainly leverage the functionalities of some other plugins installed in WordPress. For achieving this WordPress plugin needs to be connected to one or more WP Plugins which is called “Hooks”. The way Plugin hooks work is that at various times while WordPress is running, WordPress checks to see if any Plugins have registered functions to run at that instance. If any plugin is supposed to run, it starts running. Needless to say that; the execution of these plugins would certainly change the behavior of WordPress as per the requirement. These Hooks are of two types. Action and Filters.

Action

Action offers a way for any function to let the other function hook into itself so that at any specific instance the additional code can run. Actions are counter part to filters. The way to call them is by using the function add_action() function and passing two parameters: First one is the name of the action and second one is the commands of execution which means the functions needs to be hooked. For example,

Above example can be understood as, call the function do_some_stuff() when WordPress fires the init action.

Filters:

Filters provide a way to modify a certain piece of data using one function for the other one. Filters can be called by using add_filter() function. The function needs two parameters: Name of the filter you want to hook into, and the name of your callback function that will be executed when the filter runs. For example,

The above piece of code will call the modify_the_filter() function when WP applies the techtic_title filter.

 

Plugin Activation deactivation hook:

The activation and deactivation hooks provide a way to perform actions when a plugin is either activated or deactivated.

The first parameter of each function points to the file where the authorship and plugin name is defined. These two functions are usually defined in the main file itself, however it is important to understand that if these functions are defined in some other files then the first parameter needs to be modified to point to the main file.

Shortcode

How would you call a shortcode into the page or a post or anywhere? Well WordPress offers an API called Shortcode API, a simple set of functions to create the macro code for executing it in post or page content. Minimal example or rather a code of PHP required to create a Shortcode:

Example

 

Summary
Article Name
Building WordPress Plugin Development
Description
Plugins enables the website to create the features or modify them without changing the core files of the WordPress. Learn how to build plugin in WordPress.
Author
Publisher Name
Techtic Solutions
Publisher Logo
Categories: Webbing DevelopmentWebbing Wednesdays
Tags: How to create WP Plugin Plugin Development in WordPress What is WP Plugin Wordpress Plugin Development WP Plugin Creation