Thread Rating:
  • 1 Votes - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Creating a MyBB Plugin
Author Message
dhfola
Junior Member


Posts: 31
Joined: Oct 2008
Reputation: 0
Post: #1
Creating a MyBB Plugin
Its Basic making plugins but you gotta know how.

This is a basic tutorial.

lets start. Create a new file in inc/plugins/yourpluginname.php.

now start by opening and closing PHP Tags, and add your copyright.

PHP Code:
<?php
/**
 * YourPlugin 1.0
 * Ã‚© 2010 Dhfola
 * http://www.ismybb.info
 */
?>

Its REALLY Important to add

PHP Code:
if(!defined("IN_MYBB"))
{
    die(
"You Cannot Access This File Directly");


So your file is


PHP Code:
<?php
/**
 * YourPlugin 1.0
 * Ã‚© 2010 Dhfola
 * http://www.ismybb.info
 */
if(!defined("IN_MYBB"))
{
    die(
"You Cannot Access This File Directly");
}
?>

Now lets add the plugin Info Function When making a plugin Make sure you use the file name lets say i named my file yourplugin.php.

PHP Code:
function yourplugin_info()
{


PHP Code:
return array(
        
"name"  => "",
        
"description"=> "",
        
"website"        => "",
        
"author"        => "",
        
"authorsite"    => "",
        
"version"        => "",
        
"guid"             => "",
        
"compatibility" => ""
    
); 

Name => The name of your Plugin eg. Your Plugin
Description => The Description Of Your Plugin eg. This Plugin Is a Test Plugin
Website => The Plugin Site eg. http://www.yoursite.com/YourPlugin
Author => Your Name eg. Dhfola
Author Site => Your Website eg. http://www.yoursite.com/
Version => The Plugin Of Your Plugin eg. 1.0
GUID => This is for Version Checking. You Get It From MyBB Mods Site. This can be blank, No Example
Compatibility => The Version Of MyBB It Is Compatible with. eg *

So Now you know how to fill this out. Add To the file in yourplugin_info

PHP Code:
<?php
/**
 * YourPlugin 1.0
 * Ã‚© 2010 Dhfola
 * http://www.ismybb.info
 */
if(!defined("IN_MYBB"))
{
    die(
"You Cannot Access This File Directly");
}

function 
yourplugin_info()
{
return array(
        
"name"  => "YourPlugin",
        
"description"=> "This is a Example Plugin",
        
"website"        => "http://www.ismybb.info",
        
"author"        => "Dhfola",
        
"authorsite"    => "http://www.ismybb.info",
        
"version"        => "1.0",
        
"guid"             => "",
        
"compatibility" => "*"
    
);
}
?>

Easy? Well Now When the user activates the plugin this function will call.

PHP Code:
function yourplugin_activate()
  {

  } 

This is where we will add the DB's Settings Templates ect. We will add a Group Called YourPlugin and in the group there will be YourPlugin Enabled.

Really Easy to do that.

To Start

add
PHP Code:
global $db
after the { line.

PHP Code:
function yourplugin_activate()
  {
    global 
$db;
  } 

Then we make the new group in a array.

PHP Code:
$yourplugin_group = array(
        
'gid'    => 'NULL',
        
'name'  => 'yourplugin',
        
'title'      => 'YourPlugin',
        
'description'    => 'Settings For YourPlugin',
        
'disporder'    => "1",
        
'isdefault'  => 'no',
    ); 

Insert into DB

PHP Code:
$yourplugin_group = array(
        
'gid'    => 'NULL',
        
'name'  => 'yourplugin',
        
'title'      => 'YourPlugin',
        
'description'    => 'Settings For YourPlugin',
        
'disporder'    => "1",
        
'isdefault'  => 'no',
    );
 
$db->insert_query('settinggroups'$yourplugin_group);
 
$gid $db->insert_id(); 

Now lets make a new setting.
PHP Code:
$yourplugin_setting = array(
        
'sid'            => 'NULL',
        
'name'        => 'enabled_yourplugin',
        
'title'            => 'Do you want YourPlugin Enabled',
        
'description'    => 'If Yes People Can Use YourPlugin, If No People Cannot Use YourPlugin.',
        
'optionscode'    => 'yesno',
        
'value'        => '1',
        
'disporder'        => 1,
        
'gid'            => intval($gid),
    ); 

Add It To The DB

PHP Code:
$db->insert_query('settings'$yourplugin_setting);
  
rebuild_settings(); 

Thats Why We Need to global $db xD

PHP Code:
<?php
/**
 * YourPlugin 1.0
 * Ã‚© 2010 Dhfola
 * http://www.ismybb.info
 */
if(!defined("IN_MYBB"))
{
    die(
"You Cannot Access This File Directly");
}

function 
yourplugin_info()
{
return array(
        
"name"  => "YourPlugin",
        
"description"=> "This is a Example Plugin",
        
"website"        => "http://www.ismybb.info",
        
"author"        => "Dhfola",
        
"authorsite"    => "http://www.ismybb.info",
        
"version"        => "1.0",
        
"guid"             => "",
        
"compatibility" => "*"
    
);
}
function 
yourplugin_activate()
  {
    global 
$db;
    
   
$yourplugin_group = array(
        
'gid'    => 'NULL',
        
'name'  => 'yourplugin',
        
'title'      => 'YourPlugin',
        
'description'    => 'Settings For YourPlugin',
        
'disporder'    => "1",
        
'isdefault'  => 'no',
    );
   
        
$yourplugin_group = array(
        
'gid'    => 'NULL',
        
'name'  => 'yourplugin',
        
'title'      => 'YourPlugin',
        
'description'    => 'Settings For YourPlugin',
        
'disporder'    => "1",
        
'isdefault'  => 'no',
    );
 
$db->insert_query('settinggroups'$yourplugin_group);
 
$gid $db->insert_id();
   
$yourplugin_setting = array(
        
'sid'            => 'NULL',
        
'name'        => 'enabled_yourplugin',
        
'title'            => 'Do you want YourPlugin Enabled',
        
'description'    => 'If Yes People Can Use YourPlugin, If No People Cannot Use YourPlugin.',
        
'optionscode'    => 'yesno',
        
'value'        => '1',
        
'disporder'        => 1,
        
'gid'            => intval($gid),
    );
     
$db->insert_query('settings'$yourplugin_setting);
  
rebuild_settings();
  }
?>

Looks Good. Check if it works if not post here.

We Gotta now to use Hooks
[Image: text.png] View Hooks.txt


To Use Hooks use this code:

PHP Code:
$plugins->add_hook("HOOK","FUNCTION"); 

For example. this hook will add text to the index_start hook.

PHP Code:
$plugins->add_hook("index_start","hello"); 

PHP Code:
function hello(){
global 
$mybb;
if (
$mybb->settings['enabled_yourplugin'] == 1){
echo 
"<center><h1>YourPlugin Works!</h1><br />Try To Disable The Plugin :) You'll See This Text Will Hide..";
}

Ok Here is the code.

PHP Code:
<?php
/**
 * YourPlugin 1.0
 * Ã‚© 2010 Dhfola
 * http://www.ismybb.info
 */
if(!defined("IN_MYBB"))
{
    die(
"You Cannot Access This File Directly");
}
//HOOKS GOES HERE BTW
$plugins->add_hook("index_start","hello");
function 
yourplugin_info()
{
return array(
        
"name"  => "YourPlugin",
        
"description"=> "This is a Example Plugin",
        
"website"        => "http://www.ismybb.info",
        
"author"        => "Dhfola",
        
"authorsite"    => "http://www.ismybb.info",
        
"version"        => "1.0",
        
"guid"             => "",
        
"compatibility" => "*"
    
);
}
function 
yourplugin_activate()
  {
    global 
$db;
    
   
$yourplugin_group = array(
        
'gid'    => 'NULL',
        
'name'  => 'yourplugin',
        
'title'      => 'YourPlugin',
        
'description'    => 'Settings For YourPlugin',
        
'disporder'    => "1",
        
'isdefault'  => 'no',
    );
   
        
$yourplugin_group = array(
        
'gid'    => 'NULL',
        
'name'  => 'yourplugin',
        
'title'      => 'YourPlugin',
        
'description'    => 'Settings For YourPlugin',
        
'disporder'    => "1",
        
'isdefault'  => 'no',
    );
 
$db->insert_query('settinggroups'$yourplugin_group);
 
$gid $db->insert_id();
   
$yourplugin_setting = array(
        
'sid'            => 'NULL',
        
'name'        => 'enabled_yourplugin',
        
'title'            => 'Do you want YourPlugin Enabled',
        
'description'    => 'If Yes People Can Use YourPlugin, If No People Cannot Use YourPlugin.',
        
'optionscode'    => 'yesno',
        
'value'        => '1',
        
'disporder'        => 1,
        
'gid'            => intval($gid),
    );
     
$db->insert_query('settings'$yourplugin_setting);
  
rebuild_settings();
  }
function 
hello(){
global 
$mybb;
if (
$mybb->settings['enabled_yourplugin'] == 1){
echo 
"<center><h1>YourPlugin Works!</h1><br />Try To Disable The Plugin :) You'll See This Text Will Hide..";
}
}
?>

Now all we need to is deactive this plugin
PHP Code:
function yourplugin_deactivate()
  {
  global 
$db;
 
$db->query("DELETE FROM ".TABLE_PREFIX."settings WHERE name IN ('enabled_yourplugin')");
    
$db->query("DELETE FROM ".TABLE_PREFIX."settinggroups WHERE name='yourplugin'");
rebuild_settings();
 } 

Add it before the Hello Function

PHP Code:
<?php
/**
 * YourPlugin 1.0
 * Ã‚© 2010 Dhfola
 * http://www.ismybb.info
 */
if(!defined("IN_MYBB"))
{
    die(
"You Cannot Access This File Directly");
}
//HOOKS GOES HERE BTW
$plugins->add_hook("index_start","hello");
function 
yourplugin_info()
{
return array(
        
"name"  => "YourPlugin",
        
"description"=> "This is a Example Plugin",
        
"website"        => "http://www.ismybb.info",
        
"author"        => "Dhfola",
        
"authorsite"    => "http://www.ismybb.info",
        
"version"        => "1.0",
        
"guid"             => "",
        
"compatibility" => "*"
    
);
}
function 
yourplugin_activate()
  {
    global 
$db;
    
   
$yourplugin_group = array(
        
'gid'    => 'NULL',
        
'name'  => 'yourplugin',
        
'title'      => 'YourPlugin',
        
'description'    => 'Settings For YourPlugin',
        
'disporder'    => "1",
        
'isdefault'  => 'no',
    );
   
        
$yourplugin_group = array(
        
'gid'    => 'NULL',
        
'name'  => 'yourplugin',
        
'title'      => 'YourPlugin',
        
'description'    => 'Settings For YourPlugin',
        
'disporder'    => "1",
        
'isdefault'  => 'no',
    );
 
$db->insert_query('settinggroups'$yourplugin_group);
 
$gid $db->insert_id();
   
$yourplugin_setting = array(
        
'sid'            => 'NULL',
        
'name'        => 'enabled_yourplugin',
        
'title'            => 'Do you want YourPlugin Enabled',
        
'description'    => 'If Yes People Can Use YourPlugin, If No People Cannot Use YourPlugin.',
        
'optionscode'    => 'yesno',
        
'value'        => '1',
        
'disporder'        => 1,
        
'gid'            => intval($gid),
    );
     
$db->insert_query('settings'$yourplugin_setting);
  
rebuild_settings();
  }
  function 
yourplugin_deactivate()
  {
  global 
$db;
 
$db->query("DELETE FROM ".TABLE_PREFIX."settings WHERE name IN ('enabled_yourplugin')");
    
$db->query("DELETE FROM ".TABLE_PREFIX."settinggroups WHERE name='yourplugin'");
rebuild_settings();
 }
function 
hello(){
global 
$mybb;
if (
$mybb->settings['enabled_yourplugin'] == 1){
echo 
"<center><h1>YourPlugin Works!</h1><br />Try To Disable The Plugin :) You'll See This Text Will Hide..";
}
}
?>


It Works
© Dhfola
06-05-2010, 07:45 PM
euantor
Posting Freak



Posts: 888
Joined: Jun 2009
Reputation: 7
Post: #2
RE: Creating a MyBB Plugin
Nice share, thanks Dhofla!
06-05-2010, 08:50 PM
ms365
MybbSource Skinner


Posts: 98
Joined: Dec 2009
Reputation: 1
Post: #3
RE: Creating a MyBB Plugin
Great Tutorial, Thanks a bunch
And in your PHP Codes, Remove the "close tags" or users might get confused (I know thats not a mistake )
(This post was last modified: 06-06-2010, 04:53 AM by ms365)
06-06-2010, 04:52 AM
ProneLegacy
Junior Member


Posts: 21
Joined: Jun 2010
Reputation: 0
Post: #4
RE: Creating a MyBB Plugin
Awsome tutorial, I am starting to get a hang of all this now
06-06-2010, 11:44 PM
Lord Pain
Junior Member


Posts: 1
Joined: Jun 2010
Reputation: 0
Post: #5
RE: Creating a MyBB Plugin
what do i save this as .PHP? i am kinda new to mybb
(This post was last modified: 06-08-2010, 10:58 AM by Lord Pain)
06-08-2010, 10:57 AM
dhfola
Junior Member


Posts: 31
Joined: Oct 2008
Reputation: 0
Post: #6
RE: Creating a MyBB Plugin
Yes you do. YourPlugin.php. remembering to replace YourPlugin with your plugin name in the functions for example,
PHP Code:
function YourPlugin_activate()
{

If the file name is let say APlugin.php, and your function uses YourPlugin it will search for APlugin_activate() and it would return a PHP Error. Cause it searches for a function which doesn't exist.
(This post was last modified: 06-08-2010, 05:40 PM by dhfola)
06-08-2010, 05:36 PM
Don+
Member


Posts: 61
Joined: Aug 2010
Reputation: 0
Post: #7
RE: Creating a MyBB Plugin
Very good guide for beginers.
08-21-2010, 10:16 AM
1master1
Member


Posts: 89
Joined: Aug 2010
Reputation: 0
Post: #8
RE: Creating a MyBB Plugin
yeah, thats true. very useful for beginners.
02-24-2011, 08:56 AM
fma965
Member


Posts: 78
Joined: Jul 2011
Reputation: 0
Post: #9
RE: Creating a MyBB Plugin
thanks
08-01-2011, 05:56 AM
 




User(s) browsing this thread: 2 Guest(s)