Declaring options

When a visitor requests a page (whether or not it is in the cache), SPIP carries out a number of actions, one of which is to load the “options” files. In these files we can, for example, define new constants or modify global variables that control the way SPIP operates.

These options can be created in the file config/mes_options.php or in any plug-in by declaring the name of the file in plugin.xml like this: <options>pluginprefix_options.php</options>.

All options files (those of the site, and then those of all the plugins) are loaded every time a page request is made in the public zone or the private zone, so they should be as simple and as small as possible.

This example, from a contribution called “switcher”, will change the set of templates used by the site (or, strictly speaking, the name of the templates folder) depending on the value of the var_skel parameter in the URL.

<?php

// 'name' => 'template path'
$squelettes = array(
  '2008'=>'squelettes/2008',
  '2007'=>'squelettes/2007',
);

// If a particular set of templates are requested (and exist), set a cookie, otherwise delete the cookie
if (isset($_GET['var_skel'])) {
  if (isset($squelettes[$_GET['var_skel']]))
    setcookie('spip_skel', $_COOKIE['spip_skel'] = $_GET['var_skel'], NULL, '/');
  else
    setcookie('spip_skel', $_COOKIE['spip_skel'] = '', -24*3600, '/');
}

// If a particular template path is permitted, define it as the templates folder
if (isset($_COOKIE['spip_skel']) AND isset($squelettes[$_COOKIE['spip_skel']]))
  $GLOBALS['dossier_squelettes'] = $squelettes[$_COOKIE['spip_skel']];

?>

Author Thomas Sutton Published : Updated : 09/05/10

Translations : English, français