MyBB Documentation

Plugin Settings

The 1.6 Documentation is no longer maintained and some information may be outdated.

The MyBB 1.6 series reached end of life on October 1, 2015.

This means there will be no more security or maintenance releases for these series and forums running these versions of MyBB may be at risk of unfixed security issues. All administrators are strongly encouraged to upgrade their forums to the latest release of MyBB as soon as possible.

Here is how to add settings to your plugins.


Below is an example of how to add settings.

function myplugin_install()
{
	global $db;
	$setting_group = array(
		'name'			=> 'myplugin',
		'title'			=> 'MyPlugin Settings',
		'description'	=> 'Settings for MyPlugin.',
		'disporder'		=> '1', // The order that the group is displayed on the settings module
		'isdefault'		=> 'no'
	);
        $db->insert_query('settinggroups', $setting_group);
	$gid = $db->insert_id();

	$myplugin_setting = array(
		'name'			=> 'myplugin_setting1',
		'title'			=> 'A random setting for MyPlugin',
		'description'	=> 'What this setting will do',
		'optionscode'	=> 'yesno', // This will be a yes/no select box
		'value'			=> '1', // Default value is yes, use 0 for no
		'disporder'		=> '1', // The order that the settings are displayed in the group
		'gid'			=> intval($gid)
	);
	$db->insert_query('settings', $myplugin_setting);


	$myplugin_setting = array(
		'name'			=> 'myplugin_setting2',
		'title'			=> 'Another random setting for MyPlugin',
		'description'	=> 'This is a textbox.',
		'optionscode'	=> 'text', // This will be a textbox
		'value'			=> 'Lorem ipsum', // This will be the the contents of the box
		'disporder'		=> '2', // The order that the settings are displayed in the group
		'gid'			=> intval($gid)
	);
	$db->insert_query('settings', $myplugin_setting);


	$myplugin_setting = array(
		'name'			=> 'myplugin_setting3',
		'title'			=> 'Another random setting for MyPlugin',
		'description'	=> 'This is a textarea.',
		'optionscode'	=> 'textarea', // This will be a big textbox (textarea)
		'value'			=> 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.', // This will be the the contents of the box
		'disporder'		=> '3', // The order that the settings are displayed in the group
		'gid'			=> intval($gid)
	);
	$db->insert_query('settings', $myplugin_setting);


	$myplugin_setting = array(
		'name'			=> 'myplugin_setting4',
		'title'			=> 'Another random setting for MyPlugin',
		'description'	=> 'This is a select box.',
		'optionscode'	=> 'select
default=Default
one=Option One
two=Option Two
three=Option Three', // This will be an selectbox with the option to choose Default, Option One, Option Two and Option Three
		'value'			=> 'default', // This will be the value of the selectbox
		'disporder'		=> '4', // The order that the settings are displayed in the group
		'gid'			=> intval($gid)
	);
	$db->insert_query('settings', $myplugin_setting);


	// This updates the ./inc/settings.php file.
	rebuild_settings();
}

Here is how to delete settings.

function myplugin_uninstall()
{
	global $db;
	$db->write_query("DELETE FROM ".TABLE_PREFIX."settings WHERE name IN ('myplugin_setting1','myplugin_setting2','myplugin_setting3','myplugin_setting4')");

	$db->write_query("DELETE FROM ".TABLE_PREFIX."settinggroups WHERE name = 'myplugin'");
	rebuild_settings();
}


Here is an example of how you can use the above settings.

function myplugin_functionname()
{
	global $mybb;
	if($mybb->settings['myplugin_setting1'] == '1') {
		$randomvar = "<table><tr><td>{$mybb->settings['myplugin_setting2']}</td></tr></table>";
	}
}


Here is the setting group for the settings above.

Edit this page on GitHub