Tutorial - Creating a SPIP Plugin: Part 4 » SPIP Functions
Now for part 4 of the SPIP plugin tutorial. If you haven’t read the proceeding parts of the tutorial you should check out part 1, part 2 and part 3 before continuing.
In this part of the tutorial I will get a little deeper into how SPIP works and how this will affect how your plugin is made. It is important that you understand these concepts especially if you wish to create a complex plugin.
SPIP automagically performs functions using a charger function. This function takes two parameters; the name of the function and the type of function it is. The function type determines when and where it can be used. The functions are called this way so that SPIP can find the file automatically without the containing file being manually included and so that other plugins or site-specific options (eg. in mes_options.php) can override them. There are four types of these functions which will determine the plugin’s functionality:
- exec
- balise
- action
- inc
Functions of type exec are used to create backend pages, balise functions create custom tags for the front end, action functions obviously perform actions eg. processing form, create and download CSV file. These three functions perform their tasks by using ’inc’ functions and regular functions. The difference between the two is that inc functions are used by different functions in different files while regular functions are kept in the same file (or have their file included) and are used for a specific purpose relating to the function that calls it. To put it simply, exec, balise and action functions are called, they initialise variables which are then passed to the inc functions and regular functions that they need to provide their task.
When creating a function it needs to be in the format function type_name_dist(). The type is one of the four types described above. Dist is added to the end of the function for priority reasons. When SPIP looks for a function it will use the function without dist before one with it so we add dist in case another plugin or a function added to the squelettes folder can override it.
These functions are kept in a file with the same name inside the folder of that type ie. exec files are kept in the exec folder. This is done so that the charger function can find them and so that files and functions of the same name as SPIP functions or functions inside other plugins will get overridden. The order in which SPIP looks for functions and files can be found in my SPIP Override article. So your function inc_foo_dist() inside the inc/foo.php file in your plugin will be overridden by inc_foo() inside the inc/foo.php file in squelettes. This means that if a certain project requires slightly different functionality the plugin can be changed without changing the plugin code directly. The same goes for functions in squelettes or your plugin overriding SPIP code.
Action and exec functions in the backend are accessed in the URL by adding exec and action parameters with the value being the name of the function that is needed. Balise functions are accessed from the front end by using the function name as a tag in SPIP templates. These functions obviously then call inc functions to do their stuff.