Files and folders setup for creating WordPress Plugin
First, you need to create your basic files and folder structure. WordPress stores its plugins in the wp-content/plugins/ folder. This is the place where you’ll be adding your files as well. Normally, if your plugin is going to be very simple, you will include all the code inside one single PHP file. In this case, you will simply store the file in the folder mentioned above. However, for complex case, you may be using more thank one files therefore you’ll be putting all your files in a unique folder that you’ll name, let suppose you named it my_new_plugin. Go ahead and create this folder.
Create the plugin file
Next, you must create your main plugin file. Let’s suppose you name it. You can really name it whatever you want, it doesn’t make any difference.
If you now open your WordPress administration panel and navigate to the Plugins sections, you will see nothing changed yet.
It’s time to change that and tell WordPress that our file is going to implement a plugin. The process to do so is very simple. All you need to do is add a plugin specific information header to your newly created file. This standard header can be written like this:
Plugin Name: My New Plugin
Plugin URI: Enter url of you plugin.
Description: My plugin description
Author: Your name
Version: version number
Author URI: Your contact information url
WordPress offers a great way to include your plugin code in different places all over the template, be it physical positions within a page or logical positions within the process of building up a page that is going to be displayed. First, you are going to have a closer look at the second category, the logical positions, better known as action hooks.
Action Hooks for plugin
You can view action hooks as callback function. Whenever WordPress is executing a certain operation, like, for instance, displaying any page for example page footer, it will allow your plugins to execute their own code that must be run at that exact moment.
For a better understanding, let’s consider a generic plugin called my_plugin that implements a function called when_footer_displayed() that has to be run whenever the page footer is displayed. You will tell WordPress to call this function, at the moment of displaying the footer by using a special function called add_action():
<php add_action(‘wp_footer’, ‘when_footer_displayed’); ?>
The add_action() function takes the action hook name as its first parameter and the name of the function that must be executed, as a second parameter. This function call will be added to your main plugin file (the one containing the information header), usually, right under the function code that needs to be executed (when_footer_displayed() in our example). You will find the full list of available action hooks in the WordPress Codex.
Next you’ll be using action hooks , where you are going to build the administration page for your plugin.