MASTER AUTOINDEX PLUGINS DOCUMENTATION
Home » Adding Settings

Good now that you know how plugins work, where and how you can execute something it's time to see a very cool feature of mai pluging and that will be adding settings to a plugin. This will allow you to adapt what your plugin does based on what the admin of the site wants.
ok we will use the same demo_plugin.php file but make sure you go to admincp->plugin manager and deactivate it.
The settings are some amount of data that is store in the mysql database and you can access that data from your plugin and abviously use it how you need.
Usually it's recommended to add the setting on the install process so if you remember we have the demo_install() function available.
We can add settings like this:
<?php
function demo_install(){
    global 
$db// the $db object
    // settings 
    
$settings_data = array(
    
"name" => "demo_set1"// name of the setting must be unique so adding the plugin name is a good practice
    
"value" => "i\'m a default value"// default value you can also use $db->escape(); to avoid sql syntax problems
    
"title" => "Text:"// title will be displayed on settings page
    
"description" => "Enter the text that you want to display"// description
    
"type" => "text"// the setting type
    
"plugin" => "demo"// your plugin <name>
    
);
    
    
$db->insert_array(MAI_PREFIX."plugins_settings",$settings_data); // now we add it to the database
}
?>
Because most of the settings are intuitive and i will only explain the name and the type.
- name - it must be a UNIQUE name that you will use to access the settings. All the settings are hold in an array so it's very important for the name to be unique.
- type - This represents the type of the setting and it can be as follows:

Now that we made tyhe install function we also need to make the _is_installed() fuction so the admin will see the install option or else it will be skiped and the install function ignored
<?php
function demo_is_installed(){
    global 
$db;
    
// check if the plugin is installed
    // demo is the &lt;name> of the plugin
    
if($db->count("SELECT `name` FROM `".MAI_PREFIX."plugins_settings` WHERE `plugin`='demo'") > 0)
        return 
true;
    
    return 
false;
}
?>
And of course we also need to clean what we did in case the admin decides to uninstall the plugin.
<?php
function demo_is_installed(){
    global 
$db;
    
// check if the plugin is installed
    // demo is the &lt;name> of the plugin
    
if($db->count("SELECT `name` FROM `".MAI_PREFIX."plugins_settings` WHERE `plugin`='demo'") > 0)
        return 
true;
    
    return 
false;
}
?>
Now to you know how to save a setting but the question remains how to access it ?
Well that is easier then you think all you have to do is this
$set->plugins["demo_set1"];
as you can see the demo_set1 is the name that you set when you inserted the setting in the database. This is why it has to be unique.
Tip: Always remember that you have to use the global $var in order to use it in a function with it's value from outside.

You can find an example of the demo plugin with settings here.
« Back to Assign functions to hooks |
Mihai Ionut Vilcu ([email protected]) Feb - 2013