Publishing System Settings Logout Login Register
Getting Started with MyBB Plugins
TutorialCommentsThe AuthorReport Tutorial
Tutorial Avatar
Rating
Add to Favorites
Posted on June 30th, 2007
6500 views
PHP Coding
Introduction

In this tutorial, we'll take a look at the coding behind a plugin, and get to work on creating your own.

Requirements

- Some Knowledge of PHP
- The latest version of MyBB
- An editor (NotePad works just fine)

Basics of a Plugin

A plugin file in MyBB generally has the following functions:

- FILENAME_info()
- FILENAME_activate()
- FILENAME_deactivate()

(FILENAME is the name of the file). People sometimes need to add other functions to the file for the plugin to work, but a plugin file pretty much always have those functions.

FILENAME_info()

This function returns info about the plugin, inluding the plugin name, the author, and the current version of the plugin.

FILENAME_activate()

This function runs when the plugin is activated in the Admin CP. Some plugins have nothing in this function, while some do things like create a MySQL table, or edit a template here.

FILENAME_deactivate()

This function runs when the plugin is deactivated in the Admin CP. If the above function is empty, then this will most likely be empty also. In tables were created, or if templates were edited, they will be either deleted, or reverted to the originals.

Creating the Plugin

Okay, so you know what the basic functions are that a plugin requires. Now, you're just itchin' to make your own. But what's the exact coding that needs to be added?

At the beginning of the page, it is a good idea to comment in some copyright info, such as the author, version number, and creation date.

<?php
/**
 @ Author: LegosJedi
 @ Created: 04/09/07
 @ Version: 1.0
 @ Contact: [email protected]
*/
?>


Some people also include code that would prevent people from loading the file right in their browser. To do this, add something like this:

<?php
if(!defined("IN_MYBB"))
{
    die("You cannot directly access this file.");
}
?>


FILENAME_info()

<?php
function modcp_info()
{
    return array(
        "name"        => "PLUGIN NAME",
        "description"   => "PLUGIN DESCRIPTION",
        "website"    => "PLUGIN WEBSITE",
        "author"     => "AUTHOR",
        "authorsite"    => "AUTHOR WEBSITE",
        "version"     => "VERSION NUMBER",
    );
}
?>


The other two functions can have pretty much whatever you want to put into them that makes the plugin work.

Hooks

Sometimes, you need to edit pages to get your plugin to work. One of the great things with MyBB is their hooks system. Basically, it works like this:

On pretty much every page, there is code that runs functions associated with a certain phrase. By adding a piece of code that adds a function to that phrase, that function will run with all the other functions that are run by that phrase.

Adding Hooks

To add a hook to a phrase, you need to insert a piece of code that looks something like this (Taken from the MyBB "Hello World" plugin):

<?php
$plugins->add_hook("pre_output_page", "hello_world");
?>


Now, in the plugin file, there should be a function called 'hello_world'. It will be executed when the functions associated with that pharse are run (For a full list of hooks, check here).

Conclusion

That should help you start writing your own plugin for MyBB. If you have any questions about this tutorial, feel free to contact me at the MyBB Community Forums, or at the MyBB Mods Community Forums.

Copyright Info

This tutorial is copyright 2007 - present LegosJedi. You may not post this tutorial anywhere else without the author's permission.
Dig this tutorial?
Thank the author by sending him a few P2L credits!

Send
Jason C. Martin

LegosJedi is an experienced web developer, with knowledge in HTML, CSS, PHP, MySQL, and JavaScript. He is working on Cloud-BB, a bulletin board, with a friend.
View Full Profile Add as Friend Send PM
Pixel2Life Home Advanced Search Search Tutorial Index Publish Tutorials Community Forums Web Hosting P2L On Facebook P2L On Twitter P2L Feeds Tutorial Index Publish Tutorials Community Forums Web Hosting P2L On Facebook P2L On Twitter P2L Feeds Pixel2life Homepage Submit a Tutorial Publish a Tutorial Join our Forums P2L Marketplace Advertise on P2L P2L Website Hosting Help and FAQ Topsites Link Exchange P2L RSS Feeds P2L Sitemap Contact Us Privacy Statement Legal P2L Facebook Fanpage Follow us on Twitter P2L Studios Portal P2L Website Hosting Back to Top