Declaring new functions

The “_fonctions” files are loaded automatically by SPIP, but — unlike the “_options” files — only when it needs to evaluate a template to generate a new page.

These files make it possible, for example, to define new filters that can be used in templates. If you create a squelettes/mes_fonctions.php file containing the following code, then you will be able to use the hello_world filter in your templates (useless though it is!):

function filtre_hello_world($v, $add){
	return "Title:" . $v . ' // Followed by: ' . $add;
[(#TITRE|hello_world{this text is added afterwards})]

(displays "Title:title of the article // Followed by: this text is added afterwards")

To create such files in a plug-in, you need to add the name of the file in your plugin.xml like so: <fonctions>pluginprefix_fonctions.php</fonctions>. Each plug-in may contain any number of these declarations (and files).

Functions for specific templates

Sometimes, filters are specific to a single template. It is not always desirable to load all such functions for each and every page. SPIP thus makes it possible to load certain functions only when calculating a particular template.

Such a file should be created in the same folder as the template and named after it, but with _fonctions.php instead of .html.

Consider the example from above again. If the file named squelettes/world.html contains the code [(#TITRE|hello_world{this text is added afterwards})], then the hello_world function could be declared in the squelettes/world_fonctions.php file. This file will only be loaded when SPIP is generating a page based on the squelettes/world.html template.

Author Thomas Sutton Published : Updated : 12/03/23

Translations : English, français