Beginner’s Guide For Developing WordPress Plugin

commas-left

So, you are thinking to develop WordPress Plug-in for your site.

Do you know where to start from?

Read this simple guide that gives information about developing robust plug-in for WordPress CMS.

commas-right

Wordpress Plugins 2014

WordPress CMS is one of the most excellent platforms as it has ability to extend its functionality through plug-ins. Through plug-ins, you can simply perform any function to your site as per your business’s needs. We know that developing WordPress plug-in is bit difficult, but WP has excellent solution so get ready to take advantage of building WP plug-in by your own.

As we all know that WordPress development is done in PHP that makes accessible for developers to create plug-ins as they are well-aware of it. Creating plug-ins is one of the easiest tasks as one can have to create a basic file structure and have to use WordPress developed functions and event that helps to create your application.

The File Structure

A single PHP file is developed to create a plug-in; however, you have to create a house of your plug-in within a directory. Started with creating a directory, but you have to make sure to give name that matches with your plug-in. Now, within directory, create the main PHP file and named similar to your plug-in name.

+ super-plugin
    - super-plugin.php

If your plug-in needed any javascript, css, images, or additional PHP files, then it is advisable to house for those as well within this directory.

+ super-plugin
    + admin
         - super-plugin-admin.php
    + img
         - icon.png
    + js
         - super-plugin.js
- super-plugin.php
- readme.txt

This example has 3 directories, admin, img, and js to hold extra files. With super-plugin-admin.php file, one can get a web interface into plug-in on the WP backend which will be addressed in another post. One can have to add a file named readme.txt at the root of the directory, if they have planned to host plug-in in the WordPress plug-in directory. The format detailed should be followed by the file in this readme.txt example.

Defining Your Plugin

In next step, one can have to define plug-in so that WordPress will identify it and allows it to be installed, removed, and activated. It’s time to open your main plugin file, super-plugin.php and add the given to the top of the file.

<?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
*/
?>

Note: You have to replace each line with the data for your plug-in. It is will define your plug-in details for WordPress. Now, you require the Plugin Name: field for the plugin to be identified.

Initializing your program

Now, WordPress knows about the plug-ins, so it is a time to make it do something by using hooks and predefined WordPress functions. A hook is an event listeners that is trigger on the basis of outside events occurring. One can easily tap into the plug-in activation hook for performing any type of setup, such as database table creation.

//SETUP
function super_plugin_install(){
    //Do some installation work
}
register_activation_hook(__FILE__,'super_plugin_install');

After that, when your plugin is activated by any user, any function that has been registered with the activation hook will be performed. Moreover, you might want to register some custom JavaScript that your plug-ins will require to operate. One can easily do this with add_action hook to piggyback on to another process. If you are going to listen for the wp_enqueue_scripts event to trigger, so let WordPress knows that you want in on the script party and to please execute our function as well.

//SCRIPTS
function super_plugin_scripts(){
    wp_register_script('super_plugin_script',plugin_dir_url( __FILE__ ).'js/super-plugin.js');
    wp_enqueue_script('super_plugin_script');
}
add_action('wp_enqueue_scripts','super_plugin_scripts');

The function will be executed and script will get registered and queued to be added into the head portion of the HTML when scripts are being loaded up. Lastly, you have to run your program by listening to various WP hooks and waiting for a suitable event that would generate your code to run.

Users can use ‘init’ or ‘wp_loaded’ action, if they want to run your program every time a visitor comes to your site by triggering their code:

//HOOKS
add_action('init','super_plugin_init');
/********************************************************/
/* FUNCTIONS
********************************************************/
function super_plugin_init(){
    //do work
    run_sub_process();
}

function run_sub_process(){
    //more work
}

One can easily called the super_plugin_init() function on every new request and can also perform any logic he/she wants to in this example. Moreover, you can also add an extra PHP files to develop your application or can also use this initial hook to trigger the full application. Click here to get a full list of the actions that run during a typical request.

Fill out our WordPress plugin development inquiry to get started today!

work with us

Leave a Reply

Your email address will not be published. Required fields are marked *

CAPTCHA

*