Contents |
When developing plugins, you can use predefined MyBB methods and objects.
Some of these objects may be available or not, depending on what hook your plugin code is run by. For example, $forum
is available only in build_forumbits_forum
hooks, if it wsa passed as a parameter.
To explore the properties of an object, use the PHP function print_r.
MyBB uses the $db variable to perform various database functions. These range from writing to deleting queries. For a full list of available methods, see the Database Methods page.
$lang calls on language settings. If you're creating a plugin, or creating custom code that you plan on redistributing, you should always use language variables. This allows your coding to be translated into the many languages MyBB users speak. Language files are found in ./inc/languages/{language_pack}/. To create your own language file, simply make a standard PHP file (with <?php tags) and use variables like this:
$l['isloggedin'] = "This user is logged in."; $l['isnotloggedin'] = "This user is not logged in.";
Save your file in this format: "foobar.lang.php". To be able to use your language files, you need to load them first. You do this like so:
function foo_dosomethingcool() { global $lang, $mybb; $lang->load("foobar"); if($mybb->user['uid']) { return $lang->isloggedin; } else { return $lang->isnotloggedin; } }
So basically, if this function is used then it will load the "foobar.lang.php" language file and if a user is logged in, it will return the isloggedin text. If not, it will return the isnotloggedin text. We can also use the PHP Sprintf function in the $lang variable too. This lets us format a certain language string in the way we want to. For example, a language variable of
$l['welcome_back'] = "<strong>Welcome back, {1}</strong>. You last visited: {2}";
We can format this string by using $lang->sprintf:
$lang->welcome_back = $lang->sprintf($lang->welcome_back, $mybb->user['username'], $lastvisit);
$mybb->user['username'] will replace the {1}, and $lastvisit (the time of the user's last visit) will replace the {2}. That means the string will read: Welcome back, User. You last visited: Yesterday, 12:32pm (for example).
If you're stuck with language settings, you can get help at the MyBB Community Forum.
The $mybb object can be used (amoung other things) to access cookies, user and usergroup details and settings.
- $mybb->cookies['cookiename']
- Replace 'cookiename' with the name of a cookie (example: mybbuser will return the contents of the mybbuser cookie).
- $mybb->user['fieldname']
- Replace 'fieldname' with the name of a field in the Users Table in the database (example: username will return the contents of the username field in the database).
- $mybb->usergroup['fieldname']
- Replace 'fieldname' with the name of a field in the Usergroups Table in the database (example: issupermod will be able to tell you if the user is a Super Moderator or not (returns 1 if true, 0 if false).
- $mybb->settings['fieldname']
- Replace 'fieldname' with the name (not title) of a setting (example: bburl will return the board URL).
Returns the templates manager. The standard idiom for evaluating a template is:
eval("\$string_our_plugin_exports = \"".$templates->get("our_plugin_template")."\";");
Returns the current thread object.
Comma-separated list of thread IDs, constructed by forumdisplay.php
.
TBD - which thread IDs are these? those visible in the current page of the same forum as the current thread?
Replaces text inside templates. Useful to insert your own variables.
Example (from the I'm Subscribed plugin):
('forumdisplay_thread', '#{\$attachment_count}#', '{$subscription}{\$attachment_count}');