Posted on Leave a comment

Process of writing MyBB Plugins: Part 1

PHP in MyBB

You have installed the latest MyBB and got your favorite theme and plugins from MyBB Extend. Now you want to enhance your forums with custom features and you don’t find such features on MyBB Extend. Luckily you have PHP knowledge and a bit of SQL related experience and you want to get your hands dirty in writing a plugin. If you don’t have experience related to PHP and SQL, you may want to check MyBB community forums for plugin requirements. This article is for you to learn, debug and create features by writing MyBB Plugins for your forums.

Plugins in MyBB

MyBB Plugins are composed of a PHP file(mandatory plugin file), some language variables and some additional functions that are complex for a single plugin file. The mandatory plugin file is present inside inc/plugins. Suppose your plugin name is myfeature, than your plugin will be having the name myfeature.php . The file should be available inside inc/plugins/myfeature.php

Structure of Plugins

A default structure of MyBB PHP file consist of three main parts:

  • Plugin Information
  • Plugin Activation and Deactivation functions
  • Plugin Functions

Here is a MyBB guide which shows about the basic structure of MyBB Plugin. For our myfeature, our plugin will have following skeleton

<?php

// Disallow direct access to this file for security reasons
if(!defined("IN_MYBB"))
{
	die("Direct initialization of this file is not allowed.");
}


function myfeature_info()
{
	return array(
		"name"			=> "myfeature for MyBB",
		"description"	=> "Myfeature for MyBB",
		"website"		=> "https://wallbb.co.uk",
		"author"		=> "WallBB",
		"authorsite"	=> "https://wallbb.co.uk",
		"version"		=> "1.0",
		"guid" 			=> "",
		"compatibility" => "1*"
	);
}

function myfeature_install()
{

}

function myfeature_is_installed()
{

}

function myfeature_uninstall()
{

}

function myfeature_activate()
{

}

function myfeature_deactivate()
{

}

Note: The functions listed above must be prefixed with the name of the plugin file.

In the above plugin, we have 6 functions however we can use only myfeature_info and myfeature_activate and myfeature_deactivate functions for a basic plugin. _info is the mandatory function for a plugin to work and the fields inside _info should be edited depending on the plugin.

Now we have got the basic structure, let’s write a small function and insert that on portal.php. To run a custom code on any default MyBB page, we have hooks and using hooks we can inset our code without modifying the default MyBB file. This also allows to keep the default MyBB structure intact. In this sample case, we are using the hook portal_end.

$plugins->add_hook('portal_end','myfeature_portal_run');

function myfeature_portal_run() {

}

In the above code, we added a hook to portal_end and we run a function myfeature_portal_run. Let’s write a small function to print a message only on portal page.

function myfeature_portal_run() {
    $str = 'Hello, I run only on portal page';
    echo $str. "<br>";
}

In the above function, we print the string using $str variable on portal page. (We can use MyBB templates which is preferred but for example, we use print the raw $str on portal page). Here is the complete code of myfeature.php

<?php

// Disallow direct access to this file for security reasons
if(!defined("IN_MYBB"))
{
	die("Direct initialization of this file is not allowed.");
}


function myfeature_info()
{
	return array(
		"name"			=> "myfeature for MyBB",
		"description"	=> "Myfeature for MyBB",
		"website"		=> "https://wallbb.co.uk",
		"author"		=> "WallBB",
		"authorsite"	=> "https://wallbb.co.uk",
		"version"		=> "1.0",
		"guid" 			=> "",
		"compatibility" => "1*"
	);
}

function myfeature_activate()
{

}

function myfeature_deactivate()
{

}

$plugins->add_hook('portal_end','myfeature_portal_run');

function myfeature_portal_run() {
    $str = 'Hello, I run only on portal page';
    echo $str. "<br>";
}

Save the file and you will see the plugin inside Admin CP > Plugin > Inactive Plugins

Plugin showing inside admin CP.

Activate this plugin and you will see the message Hello, I run only on portal page only on the portal page.

Congratulations, you have written your very first plugin for your awesome MyBB forum. In the next part, we will talk about using MyBB default features in plugins and also how to use templates.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.