MyStudio modules expose PHP functions and MyBB hooks that your own modules and themes can use: charge members through Payments, add or spend points, send alerts, add permalink prefixes and sitemap entries, and react to orders, licence keys, likes and awards.
- Check it exists with
function_exists(): the module may be switched off
Always check first
Every module can be switched off, so check that a function exists before calling it:
if (function_exists('ms_pay_create') && ms_pay_available('EUR')) { … }
Payments: charge a member
Work out the amount on the server from your own data, never from the browser:
$r = ms_pay_create(array(
'context' => 'acme', // your module's key
'ref' => 'order-' . $orderId, // your id for what is paid for
'description' => 'Order #' . $orderId, // shown to the payer
'amount' => '19.99', // or 'amount_minor' => 1999
'currency' => 'EUR',
'return_url' => 'misc.php?action=acme_order&id=' . $orderId,
'meta' => array('order' => $orderId),
));
if (!empty($r['error'])) error(htmlspecialchars_uni($r['error']));
header('Location: ' . $r['url']);
exit;
Then act on the outcome with hooks. They fire on the payer's return, on webhooks and on Admin CP refunds:
| Hook | Arguments | When |
|---|---|---|
ms_payments_completed | txn | Money received; fires exactly once |
ms_payments_refunded | txn, amount, refund_id | Each refund |
ms_payments_reversed | txn | Chargeback: revoke what was bought |
ms_payments_failed | txn | Declined |
ms_payments_status | txn, from, to | Every status change |
$plugins->add_hook('ms_payments_completed', 'acme_paid');
function acme_paid(&$args)
{
$txn = $args['txn'];
if ($txn['context'] !== 'acme') return;
// fulfil $txn['meta']['order']
}
Helpers: ms_pay_available(), ms_pay_is_paid($context, $ref), ms_pay_find(), ms_pay_get(), ms_pay_refund(), ms_pay_format($minor, $currency), ms_pay_to_minor(). Amounts are stored as integers in minor units. The Shop and the Ads Manager's ad sales are complete examples.
Points
if (function_exists('ms_points_change')) {
// add 25 points, or spend with a negative amount (refused below 0)
ms_points_change($uid, 25, 'acme_bonus', 'Bonus for your review', 'review-' . $id);
echo ms_points_format(ms_points_balance($uid));
}
Every change is logged; the hook ms_points_changed (uid, amount, balance, type, ref) fires after each one.
Alerts
if (function_exists('ms_alerts_add_many')) {
ms_alerts_add_many(array($uid), 'reply', $fromUid, $pid);
}
Alert types are defined in User Profile Extras. Themes print the bell with {$ms_alerts_header}.
SEO: addresses and the sitemap
ms_routes_prefixes(filter): register a path prefix for your pages, such asacme→misc.php?action=acme&item=. The SEO module writes the server rules and rewrites links.ms_route_url($plain): the pretty address for a plain one. Checkfunction_existsfirst.ms_sitemap_sources(filter): add a list of your pages to the XML sitemap.ms_modules_loaded: fires once every module is loaded.
Other hooks MyStudio fires
| Hook | When |
|---|---|
ms_shop_order_paid, ms_shop_order_refunded, ms_shop_order_status | Shop orders change |
ms_shop_product_extra | Add content to a product page |
ms_shop_sub_access | A subscription's access changes |
ms_payments_sub_status, ms_payments_sub_payment | Gateway subscriptions change or charge |
ms_lic_key_issued, ms_lic_key_status | Licence keys are issued or change status |
ms_pex_liked | A post is liked |
ms_award_given | A member receives an award |
Theme-side variables
$mybb->ms_options: the admin's layout choices, checked and filled with defaults.$mybb->ms_theme_options: the active theme's settings.$GLOBALS['ms_nav_links']and$GLOBALS['ms_usercp_links']: module links for menus.
Collections have their own query API: see querying collections.