How create new wordpress plugin?

1. Introduction

WordPress is gaining more and more popularity each day, not just as a blogging platform but also as a basic CMS, thus improving and extending its basic functionality becoming a day-to-day necessity for a lot of developers. Fortunately, the WordPress developers have foreseen these needs and added the possibility of customizing the basic functionality by adding plugins. Basicaly, a WordPress plugin is a (more or less) stand-alone piece of code that can be executed in different sections and stages within a page or site.

Let’s get started!

2. Files and folders

First, we’ll need to create our basic files and folder structure. WordPress stores its plugins in the wp-content/plugins/ folder. This is the place where we’ll be adding our 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, in our case, we are going to use two files (one for the main plugin file and one for implementing the administration page) therefore we’ll be putting all our files in a specific folder that we’ll name my_plugin. Go ahead and create this folder.

3. Create plugin file

Next, we must create our main plugin file. We’ll name it my_plugin.php. You can really name it whatever you want, it doesn’t make any difference.

Now we will create information, wicht we will see in administration plugins section. All we need to do is add a plugin specific information header to our newly created file. This standard header will look like this:

  1. <?php
  2. /*
  3. Plugin Name: My plugin display
  4. Plugin URI:
  5. Description: Plugin description
  6. Author: author name
  7. Version: 1.0
  8. Author URI:
  9. */
  10. ?>
Simple enough, don’t you think? You can, of course, change the content of this header to your liking but make sure you keep all the lines, otherwise WordPress won’t correctly recognize your plugin.
4. Work with action hooks

Our plugin is now shown in the administration panel so WordPress is aware of it. However, it doesn’t do anything as it contains nothing except of the information header. We are going to change this now.

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, we are going to have a closer look at the second category, the logical positions, better known as action hooks.

Action Hooks

You can view action hooks as callback function. Whenever WordPress is executing a certain operation, like, for instance, displaying the 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 mp_footer() that has to be run whenever the page footer is displayed. We will tell WordPress to call this function, at the moment of displaying the footer by using a special function called add_action():

  1. <php add_action(‘wp_footer’, ‘mp_footer’); ?>

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 (mp_footer() in our example). You will find the full list of available action hooks in the WordPress Codex.

We’ll be using action hooks in the next chapter, where we are going to build the administration page for our plugin.

5. Creating the plugin’s administration page

If you’ll scroll over the list of action hooks, you’ll see that WordPress also provides one that gets called when the basic menu structure has been generated (admin_menu) so, this would be the optimal place to chime in and create our own menu item.

Now that we identified the action we are going to use, all we need is to define our own function that will be called when this action hook runs. We’ll call our function oscimp_admin_actions() where oscimp_ stands for oscommerce importer and is used to create a possibly unique function name that will not get mismatched with any other function within WordPress or any of its plugins. Let’s see how the code will look like:

  1. function my_plugin_admin_actions() {
  2. }
  3. add_action(‘admin_menu’, ‘my_plugin_admin_actions’);

As you can see, we are creating our function oscimp_admin_actions() then associate it with the admin_menu action hook using the add_action() function. The next step would then be to add some code to our my_plugin_admin_actions() function to actually create the new menu item.

As with most WordPress things, adding a new menu item is also very easy. It all boils down to calling a single function. We would like to add our new menu item to the Settings menu so, in this case the function we need is add_options_page(). We’ll add the code inside the oscimp_admin_actions() function.

  1. function my_plugin_admin_actions() {
  2. add_options_page(“My plugin Display”, “My plugin Display”, 1, “My plugin Display”, “my_plugin_admin”);
  3. }
  4. add_action(‘admin_menu’, ‘my_plugin_admin_actions’);

If you refresh your admin page, you’ll see the new menu item appear under Settings.

Each existing menu has its own function to be used to add sub-menu items. For instance, if we would like to add our sub-menu item to the Tools menu instead of Settings, we would use the add_management_page() function instead of add_options_page(). You can find more details about the available options in the Adding Administration Menus section of the WordPress Codex.

If we get back to the newly added code line, you’ll probably notice the last parameter. This is actually a function name that will be called when the newly added menu item is clicked on and will be used to build the administration page of our plugin. Next, we’ll be adding this new function. However, before proceeding we should stop for a moment and think about what will be implemented on this page.

We already defined the parameters we want to make configurable (database host, name, user, etc) so these will have to be included in a form in order to allow the user to send the data to the database. Once the form is defined, we’ll need a bit of code that extracts the sent data from the form and saves it to the database. Last but not least, we need some code to extract the existing data from the database (if any) and pre-populate the form with these values. As you can see, there are quite a few things to do so, it might be a good idea to separate this functionality to its own file. We’ll name the file my_plugin_admin.php. Now, go and create an empty file with the given name.

As already mentioned, we’ll have to create the function that will display our plugin configuration page (we named this function my_plugin_admin()). The code inside this function will be included from our newly created PHP file, my_plugin_admin.php

  1. function my_plugin_admin() {
  2. include(‘my_plugin_import_admin.php’);
  3. }
  4. function my_plugin_admin_actions() {
  5. add_options_page(“My plugin”, “My plugin”, 1, “My plugin Display”, “my_plugin_admin”);
  6. }
  7. add_action(‘admin_menu’, ‘my_plugin_admin_actions’);

If you now click on the link under the Settings menu, you will be directed to an empty page. This is because our my_plugin_import_admin.phpfile is still empty.

Next, we are going to create what we want.

I hope this helps for you.

1 comment

Leave a Reply

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