Create a MyStudio module: a MyBB plugin with settings

amtekkie · Today

A MyStudio module is a self-contained feature for MyBB that admins switch on and off: one folder with a plugin.json, an init.php that registers MyBB hooks, and optionally a settings form, strings, templates, CSS, JavaScript and its own Admin CP page. This guide builds a small announcement bar module.

You'll build
  • plugin.json → settings form → init.php → CSS → switch it on
  • A small announcement bar module

Module or classic MyBB plugin?

MyStudio moduleClassic MyBB plugin
Lives ininc/plugins/mystudio/modules/{id}/inc/plugins/{name}.php
Switched on inMyStudio → ModulesConfiguration → Plugins
Settings formA short list in options.php; MyStudio builds the formSetting groups and settings created by your code
CSS and JSDrop files in css/ and js/; they load automaticallyEdit templates or inject tags yourself
Install codeNot needed for most modulesRequired

Write a module for MyStudio-powered forums; write a classic plugin when it must run on any MyBB forum.

What a module looks like

inc/plugins/mystudio/modules/acme-announce-bar/
├── plugin.json        name, version, description (required)
├── init.php           your PHP: registers MyBB hooks (required)
├── options.php        the settings form (optional)
├── default.json       shipped default values (optional)
├── admin.php          an extra Admin CP page (optional)
├── lang/en/           strings
├── templates/         templates, installed as MyBB global templates
├── css/               loaded on every page while the module is on
└── js/                loaded on every page while the module is on

The folder name is the module's id. Prefix it with your own name to avoid clashes.

plugin.json

{
    "name": "Announcement Bar",
    "icon": "bi-megaphone",
    "description": "Shows a dismissible message across the top of every page.",
    "version": "1.0.0",
    "author": "Your Name"
}

Optional flags: "standalone": true (loads under every theme, for site tools), "menu_group" (design, modules or system) and "menu_order" for its place in the Admin CP menu, "assets_when" (load CSS and JS only on pages containing given markers) and "js_defer": false. Modules start switched off.

The settings form

<?php
if (!defined('IN_MYBB')) {
    die('Direct initialization of this file is not allowed.');
}
return array(
    array('id' => 'message', 'label' => 'Message', 'description' => 'Leave empty to hide the bar.',
          'type' => 'text', 'default' => 'Welcome! Read the forum rules before posting.'),
    array('id' => 'colour', 'label' => 'Background colour', 'type' => 'color', 'default' => '#0d9488'),
);

Types: text, textarea, yesno, select, radio, color, numeric, image, icon_chooser. Mark API keys 'secret' => true: they're never exported. 'requires' => 'ms-points' hides a setting while another module is off.

init.php

<?php
if (!defined('IN_MYBB')) {
    die('Direct initialization of this file is not allowed.');
}
global $plugins;
$GLOBALS['announce_bar_options'] = $ms_plugin_options;   // this module's saved settings

if (!defined('IN_ADMINCP')) {
    $plugins->add_hook('pre_output_page', 'announce_bar_output');
}

function announce_bar_output($page)
{
    $opts = $GLOBALS['announce_bar_options'];
    $message = trim($opts['message'] ?? '');
    if ($message === '') return $page;
    $message = htmlspecialchars_uni($message);
    $colour = preg_match('/^#[0-9a-fA-F]{3,8}$/', $opts['colour'] ?? '') ? $opts['colour'] : '#0d9488';
    $bar = '<div class="announce-bar" style="background:' . $colour . '">' . $message . '</div>';
    return preg_replace('#<body(\s[^>]*)?>#i', '$0' . $bar, $page, 1);
}

init.php also gets $ms_plugin_dir, $ms_plugin_id, $ms_theme_slug and $mybb->ms_theme_options.

CSS, then switch it on

Put css/announce-bar.css in the folder; it loads by itself. Add the module's id to your theme's theme.json "modules" (or make it standalone), sync the theme, then click Activate in MyStudio → Modules. Its settings page appears in the MyStudio menu.

Going further

Database tables

Create tables the first time the module runs, after checking $db->table_exists(). In this MyBB build insert_query() and update_query() don't escape values: run text through $db->escape_string() and cast numbers with (int).

Strings, templates and colours that work in any theme

  • Strings in lang/en/{id}.lang.php, used as $lang->key and {$lang->key}. Themes can override them.
  • Templates in templates/{title}.html, installed as global templates and refreshed when the files change. A theme restyles one by shipping a template with the same name.
  • Colours from the --ms-* variables (--ms-accent, --ms-border, --ms-surface, --ms-muted, --ms-text …) and icons from the shipped icon font.
  • A new template hook point in a theme needs an empty default, so themes keep working with your module off.

An Admin CP page

Add admin.php: it runs before the page header (handle posts and redirect there) and sets $ms_module_admin_render to a function that outputs the page with MyBB's Form and Table classes. Modules never change MyBB's own Admin CP pages.

  • User CP: hook usercp_start, add the link in usercp_menu, and append to $GLOBALS['ms_usercp_links'] for themes with their own menu.
  • Menu links: append to $GLOBALS['ms_nav_links'] in global_end.
  • AJAX: hook xmlhttp, use your own action name and check verify_post_check(). Limit public endpoints with MyStudio::rateLimit().

Payments, points and alerts

See module APIs.

Checklist before sharing

  1. plugin.json has a name, version and description.
  2. Every PHP file starts with the IN_MYBB check; front-end hooks sit inside if (!defined('IN_ADMINCP')).
  3. Output escaped with htmlspecialchars_uni(); database text with $db->escape_string().
  4. Every action that changes data checks my_post_key.
  5. Function names start with your module's prefix.

Share it from Themes → Export Package with (No theme: modules only). More in security rules.