Add settings to a MyBB theme: the options schema

amtekkie · Today

Theme options let forum admins change your MyBB theme without touching code: a logo, a colour, a welcome text, a toggle. You describe them in a short PHP array; MyStudio builds the Admin CP form, saves the values and hands them to your templates.

In short
  • Define options in inc/plugins/mystudio/themes/{slug}/options.php
  • MyStudio builds the form and saves values to the theme's default.json
  • Templates read them as {$mybb->ms_theme_options['key']}

Define options

Create inc/plugins/mystudio/themes/{slug}/options.php. It returns an array of options keyed by name:

<?php
if (!defined('IN_MYBB')) {
    die('Direct initialization of this file is not allowed.');
}

return array(
    'welcome_text' => array(
        'title'       => 'Welcome message',
        'description' => 'Shown at the top of the forum index.',
        'type'        => 'text',
        'default'     => 'Welcome to our community!',
    ),
    'show_banner' => array(
        'title'       => 'Show the welcome banner',
        'type'        => 'yesno',
        'default'     => '1',
    ),
);

Use the values

  • In templates: {$mybb->ms_theme_options['welcome_text']}
  • In theme PHP: $mybb->ms_theme_options['show_banner']

Values are saved in the theme's themes/{slug}/default.json. Theme files shipped with a value there act as its defaults.

<if $mybb->ms_theme_options['show_banner'] == 1 then>
  <div class="welcome">{$mybb->ms_theme_options['welcome_text']}</div>
</if>

Option types

TypeGives
text, textareaA text box
yesnoA yes/no switch ('1' or '0')
select, radioA choice; add 'options' => array('value' => 'Label')
numericA number, with optional min and max
colorA colour picker
color_presetsColour swatches plus a picker, with the live light/dark preview and contrast check
imageAn image upload (saved under uploads/)
icon_chooserThe icon chooser (about 2,000 icons)
fontThe font chooser, with Google fonts

A theme colour that stays readable

Give a colour option 'type' => 'color_presets' and a 'preview' with your palettes' backgrounds. The Admin CP then shows the live preview in dark and light mode and warns when the colour would be hard to read:

'theme_color' => array(
    'title'   => 'Theme Colour',
    'type'    => 'color_presets',
    'default' => '#2dd4bf',
    'options' => array('#2dd4bf' => 'Teal', '#60a5fa' => 'Blue', '#f472b6' => 'Pink'),
    'preview' => array('dark_bg' => '#0f172a', 'dark_surface' => '#1e293b', 'light_bg' => '#f4f6fa'),
),

'allow_empty' => "The theme's own" adds a "no colour" checkbox. In PHP, MyStudio::basePalette($hex, 'dark') builds page, panel, border and menu colours from one colour.

Fonts in theme options

A 'type' => 'font' option gets the same chooser as MyStudio Options, Google fonts included. Add 'empty_label' => 'Same as the theme' to offer the theme's own font, and 'sample_option' => 'logo_text' to draw the samples in another option's text. In PHP, MyStudioFonts::css($value) gives the CSS font-family and MyStudioFonts::linkTag($value) the stylesheet link.

Child themes and options

A child theme's options.php adds options or replaces the parent's by key. Its saved values are its own: a child theme never uses its parent's saved settings, only the defaults. See child theme development.

Options for modules

Modules use the same idea with a list of settings in their own options.php; see creating a module.