Zen-Dreams

Zen-Dreams.com

Write your own plugin for Wordpress [Chapter 3]

As a reminder, this article is the third of a series called Write your own plugin for Wordpress

  1. Introduction to plugins
  2. Plugin’s skeletton

1. Plugin activation

When a Wordpress plugin becomes active, it might be interesting to do some processing (such as database creation, options filling, and so on…). To do so, Wordpress is providing a very usefull function that will enable you to call your own initialisation function. It’s called register_activation_hook($file,$function);

This function takes these two parameters:

  • $file : the file where is located the function to call
  • $function: the name of the function to call

So you will only need to declare previous line with corresponding parameters and, of course, to have your function ready. Anything present inside this function will be executed by Wordpress engine at plugin activation time, and only at this moment. Of course, counterpart function exists, and is called register_deactivation_hook($file, $function);.

When calling register_activation_hook, you can use the __FILE__ constant value which describes current file. This will give you something like this :

register_activation_hook(__FILE__,'my_init_function');

As soon as Wordpress will receive the order to activate your plugin, this function will be executed. That means that it’s in this function that you will place your orders for database creation.

2. Important global variables

In order to use your wordpress at its best, you should use some global variables that are filled by the engine itself. This will allow you to retrieve precious information to have a better interraction with the core of WP.

The most important one is probably $wpdb; It is the variable (class) that will allow you to do some database processing. It includes table prefix, table names, etc…

3. Hooks, actions & filters

This is a concept well implemented within Wordpress and will allow you to change the behaviour of the Core. I will not give a full list of hooks, but I’ll detail what they are and how they work, along with some usefull filters.

What is a filter ?

A filter is a Hook within the core of Wordpress that will allow you to modify information before they are use by Wordpress. For example, before insertion into the database or before on-screen display. This is what I do with ZdMultilang to translate categories before they are displayed.

A filter is taking parameters and returns them modified or not.

What is an action ?

An action is also a Hook, as opposed to the filter, it does’t modify values but is executed when some events occur. For example, when your theme is changed or administration page displayed, etc…

For instance, it will allow you to add tabs to the administration panel :

As a conclusion, to create a plugin, in your skeletton, you have to use the register_activation_hook to activate your plugin, and then add hooks to manage expected behaviour.

By Anthony

Post a Comment

Your email is never shared. Required fields are marked *

*
*