How to build WordPress Plugins

Plugins are PHP scripts, software that lets users install additional features on existing programs. There are many ways through which you could potentially use a plugin. For example, you can install a plugin onto a browser and get additional features for the browser; features that were not available earlier. These programs are really important, especially if you have a website designed in platforms like WordPress and Blogspot.

WordPress plugins can boost your website, and there are thousands of such software in the WordPress store. Here is a short intro on how they can improve your website:

Plugins can be a big help in decreasing your workload. For example, if you have a regularly updated blog in your website, with thousands of subscribers, the workload is more. If you have plugins, then you can most certainly activate the plugins, instead of doing the coding. Plugins are also a major help when you eliminate the need of a professional programmer, especially when you are looking to build an entire website from scratch.

Even though there are more than 45,000 plugins in the WordPress repository, you can always build your own; this way, you can configure one to suit your requirements.

Have a plan first

If you are looking to build your own plugin, you need to have a list of the features the software will have, and what it needs to do. Once the plugin is created, it will have its own admin page, and it allows you to separate your own code from core WordPress code.

What you need for Plugin Development

Every developer has his own way of developing the plugin. You can go through the process below, and see if it suits you:

Coding Editor

An important component for plugins is the coding editor. It is a box where you edit the codes, is open source, comes mostly written in Javascript and available on both OSx and Windows.

A place to store the plugin

One of the initial steps in creating the software would be to make a folder to store the files. They can be solved in the following folder – /wp- content/plugins/. Of course, it is important to remember that the folder has to have a unique name and description, so it doesn’t clash with any other folders. Click on to the hosting account with your FTP account.

Get to the main WordPress directory, navigate to wp-content and head to the plugins folder. Now inside this folder, create another folder named my-first- plugin. If you are creating more than one plugin, then it would be better to have separate folders for each. This would help you when the plugins need to have complex features. If the program should have custom CSS, then create a CSS folder to save all the CSS files.

Create the first file

The first file is very important as it contains all the information that you need to activate the software. This new file is created in the my-first- plugin folder, and named my-first- plugin.php. WordPress will display all the information about this software in the plugin list.

Give the software a name. It will go something like this:
/*
Plugin Name: My First Plugin
*/

This software will have links to actions. Apart from that, you can add information like description of the plugin, your name, the plugin’s current version, link to your website and so on. Upload this, refresh the admin areas of the Plugins page, and you can see your creation with your name.

Everything is made into separate files and folders while developing a plugin. While the first file holds the comment headers, you can have the plugins code in separate files within individual subfolders. PHP’s ‘include’ function lets you access them. It is also a good move to give the files, functions and variables unique prefixes, so you can call them at any time and avoid conflicts.

Create a new folder called “Includes’ where the new file will have the plugins functions in them. Next go to the main folder, and include the mfp- functions.php file to enable the new functionalities. When the functions are in the main plugin file, then you can easily retrieve the functions accessible to other files in the plugin.

Next comes, the step where you configure the software so it actually use the new functions. To do that you have to back to my-first- plugin.php in your plugin’s main folder and include the mfp-functions.php file. This will let you actually use the new functions, and as it is in the main file, this would be available to other files in the plugin. This code require_once will make are the plugins work only if the function file is there.

Writing a new function entails that you write a comment describing the function, followed by Naming the function and then, Writing the function.

Once the program is made, you have to give the menu, a title and decide who is allowed to see it. There are certain parameters that will be included here:

1. The title of the page that will be displayed after clicking the link
2. Text, as the menu link, which will be shown as the name of the plugin
3. Deciding on which users have the manageability of the page
4. Which file will come up when the page is displayed. This file will be stored within the subfolder, under the name mfp-first- acp-page.php.
5. The name of the function that delivers the output.

Next, Edit mfp-functions.php, remove // My code goes here, replace it with add_menu_page() and generate the following parameters:
/*
* Add my new menu to the Admin Control Panel
*/

// Add a new top level menu link to the ACP
function mfp_Add_My_Admin_Link()
{
add_menu_page(
‘My First Page’, // Title of the page
‘My First Plugin’, // Text to show on the menu link
‘manage_options’, // Capability requirement to see the link
‘includes/mfp-first- acp-page.php’ // The ‘slug’ – file to display when
clicking the link
);
}
Source:
https://gist.githubusercontent.com/domantasg/b857fd2d7e3582f836d89c20b6764a8a/raw/
a36f02d1f1a37a9a9542a2f74e3bba34ef31edab/function%20mfp_Add_My_Admin_Link()

In order for this function to run, you have to use the WordPress function add_action(), wherein the final function will look like this

/*
* Add my new menu to the Admin Control Panel
*/

// Hook the ‘admin_menu’ action hook, run the function named
‘mfp_Add_My_Admin_Link()’
add_action(‘admin_menu’, ‘mfp_Add_My_Admin_Link’ );

// Add a new top level menu link to the ACP
function mfp_Add_My_Admin_Link()
{
add_menu_page(
‘My First Page’, // Title of the page
‘My First Plugin’, // Text to show on the menu link
‘manage_options’, // Capability requirement to see the link
‘includes/mfp-first- acp-page.php’ // The ‘slug’ – file to display when
clicking the link
);
}

Source:
https://gist.githubusercontent.com/domantasg/661b58de940f176ae5931b6d79c2cc52/raw/
8058c3a9ea0d5ba2d2f2d0bb5574f5c96d964f6c/function%20mfp_Add_My_Admin_Link()

As the last step, you can create the Admin Page. When you click on your admin control panel link, the corresponding page will display itself.

Hello!

This is my plugin’s first page

Source:
https://gist.githubusercontent.com/domantasg/577c16b95f793aab644a62c4cf66f456/raw/
5ab6f4c486c7132c305f2bead7bec22d4a26e4b5/Plugin’s%20First%20Page

Finally, you can go to the plugin list in the WordPress Admin Control Panel to activate the plugin.

This is how you create a WordPress plugin, add a link to the admin menu and fresh page in the Control panel.25% of the internet’s websites are powered using WordPress, so it is only a matter of logic if you decide to develop your own plugin.

Interesting Articles:
Basics of building WordPress plugin
Essentials to create a plugin

Picture Source: Flickr.com/ mkhmarketing


The author: Reema Oamkumar is engaged as a thought leader at www.Software-Developer-India.com which is a part of the YUHIRO Group. YUHIRO is a German-Indian enterprise which provides programmers to IT companies, agencies and IT departments.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.