REGISTER or LOGIN to have the annoying ads removed.
Thread Rating:
  • 14 Vote(s) - 4.21 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[TUTORIAL] How to make a plugin.
#1
You should start off with the basic plugin template.

Notations are in BLUE and need to be removed from final plugin product
RED text is areas that you will replace with your own information and variable for the plugin
Quote:// The information that shows up on the plugin manager
function pluginname_info()
{
return array(
"name" => "Plugin Name",
"description" => "Plugin Description",
"website" => "Plugin's Website",
"author" => "Your name",
"authorsite" => "Your website",
"version" => "Plugin Version",
);
}

// This function runs when the plugin is activated.
function pluginname_activate()
{
}

// This function runs when the plugin is deactivated.
function pluginname_deactivate()
{
}

This is explained well at the mybb wiki here:

http://wiki.mybboard.net/index.php/Authoring_Plugins

What is not explained well is exactly HOW to use the hooks and create a functioning plugin.

To add an admin setting to your plugin you first need to add a query array to your activation function.

Something like this is used to first create the "Settings Group"
Quote: require "../inc/adminfunctions_templates.php";
global $db, $mybb;

$GROUPNAME_group = array(
"gid" => "NULL",
"name" => "GROUPNAME",
"title" => "GROUP TITLE",
"description" => "GROUP DESCRIPTION",
"disporder" => "88",
"isdefault" => "no",
);

$db->insert_query(TABLE_PREFIX."settinggroups", $GROUPNAME_group);
$gid = $db->insert_id();

(note the 88 is display order and you should create a number somewhere between 30 and 250 as sometimes mybb has a conflict here and a setting won't be displayed because of this)

Next the actual settings within the group are added to your activation function.
Quote: $PLUGINNAME_setting_1 = array(
"sid" => "NULL",
"name" => "PLUGIN SETTING NAMEenable",
"title" => "Enable",
"description" => "Would you like to enable PLUGINNAME?",
"optionscode" => "yesno", Plugin types are "yesno"(radio button), "text"(single line), and "textarea".
"value" => "yes",
"disporder" => "1",
"gid" => intval($gid),
);

$db->insert_query(TABLE_PREFIX."settings", $PLUGINNAME_setting_1);

$PLUGINNAME _setting_2 = array(
"sid" => "NULL",
"name" => "PLUGIN SETTING NAME",
"title" => "PLUGIN TITLE",
"description" => "PLUGIN DESCRIPTION",
"optionscode" => "text",
"value" => PLUGIN VALUE",
"disporder" => "2",
"gid" => intval($gid),
);

$db->insert_query(TABLE_PREFIX."settings", $PLUGINNAME_setting_2);

In the above example the first setting is an "enable/disable" query. The second is a setting for the plugin. Both of these I will explain how to use in a function. But first let's finish off the deactivation part of your plugin.

Quote: require '../inc/adminfunctions_templates.php';
global $db;

$db->query("DELETE FROM ".TABLE_PREFIX."settings WHERE name IN('$PLUGIN SETTING NAME', '$PLUGIN SETTING NAME')");Here you will replace all your PLUGIN SETTING NAMES into this array
$db->query("DELETE FROM ".TABLE_PREFIX."settinggroups WHERE name='GROUPNAME'"); The groupname should be the same as the one created above in the activation

At this point your plugin will look like this:

Quote:// The information that shows up on the plugin manager
function pluginname_info()
{
return array(
"name" => "Plugin Name",
"description" => "Plugin Description",
"website" => "Plugin's Website",
"author" => "Your name",
"authorsite" => "Your website",
"version" => "Plugin Version",
);
}

// This function runs when the plugin is activated.
function pluginname_activate()
{

require "../inc/adminfunctions_templates.php";
global $db, $mybb;

$GROUPNAME_group = array(
"gid" => "NULL",
"name" => "GROUPNAME",
"title" => "GROUP TITLE",
"description" => "GROUP DESCRIPTION",
"disporder" => "88",
"isdefault" => "no",
);

$db->insert_query(TABLE_PREFIX."settinggroups", $GROUPNAME_group);
$gid = $db->insert_id();

$PLUGINNAME_setting_1 = array(
"sid" => "NULL",
"name" => "PLUGIN SETTING NAMEenable",
"title" => "Enable",
"description" => "Would you like to enable PLUGINNAME?",
"optionscode" => "yesno", Plugin types are "yesno"(radio button), "text"(single line), and "textarea".
"value" => "yes",
"disporder" => "1",
"gid" => intval($gid),
);

$db->insert_query(TABLE_PREFIX."settings", $PLUGINNAME_setting_1);

$PLUGINNAME _setting_2 = array(
"sid" => "NULL",
"name" => "PLUGIN SETTING NAME",
"title" => "PLUGIN TITLE",
"description" => "PLUGIN DESCRIPTION",
"optionscode" => "text",
"value" => PLUGIN VALUE",
"disporder" => "2",
"gid" => intval($gid),
);

$db->insert_query(TABLE_PREFIX."settings", $PLUGINNAME_setting_2);

}

// This function runs when the plugin is deactivated.
function pluginname_deactivate()
{

require '../inc/adminfunctions_templates.php';
global $db;

$db->query("DELETE FROM ".TABLE_PREFIX."settings WHERE name IN('$PLUGIN SETTING NAME', '$PLUGIN SETTING NAME')");Here you will replace all your PLUGIN SETTING NAMES into this array
$db->query("DELETE FROM ".TABLE_PREFIX."settinggroups WHERE name='GROUPNAME'"); The groupname should be the same as the one created above in the activation
}
#2
Next we will look into our hooks and functions.

Near the top of our file we are going to add an addhook line.

Quote:$plugins->add_hook("HOOK NAME", "FUNCTION NAME");

A list of hooks are available here (not complete list as it's old version):
http://wiki.mybboard.net/index.php/MyBB_Plugin_Hooks

You can also look within a file you wish to manipulate such as showthread.php and search for "hooks" and you will see them named such as
Quote:$plugins->run_hooks("showthread_start");
So at that moment in the showthread.php it will look for any hook running as showthread_start.

Some other important hook name might be these:

admin_global_start
pre_output_page
postbit

Next let's add the function to match with your hook after the activate and deactivate functions.

Quote:// Functions
function FUNCTION NAME(){

}

Next we should add our php tags to beginning and end of file.

So now our plugin should like like this:

Quote:<?php

$plugins->add_hook("HOOK NAME", "FUNCTION NAME");

// The information that shows up on the plugin manager
function pluginname_info()
{
return array(
"name" => "Plugin Name",
"description" => "Plugin Description",
"website" => "Plugin's Website",
"author" => "Your name",
"authorsite" => "Your website",
"version" => "Plugin Version",
);
}

// This function runs when the plugin is activated.
function pluginname_activate()
{

require "../inc/adminfunctions_templates.php";
global $db, $mybb;

$GROUPNAME_group = array(
"gid" => "NULL",
"name" => "GROUPNAME",
"title" => "GROUP TITLE",
"description" => "GROUP DESCRIPTION",
"disporder" => "88",
"isdefault" => "no",
);

$db->insert_query(TABLE_PREFIX."settinggroups", $GROUPNAME_group);
$gid = $db->insert_id();

$PLUGINNAME_setting_1 = array(
"sid" => "NULL",
"name" => "PLUGIN SETTING NAMEenable",
"title" => "Enable",
"description" => "Would you like to enable PLUGINNAME?",
"optionscode" => "yesno", Plugin types are "yesno"(radio button), "text"(single line), and "textarea".
"value" => "yes",
"disporder" => "1",
"gid" => intval($gid),
);

$db->insert_query(TABLE_PREFIX."settings", $PLUGINNAME_setting_1);

$PLUGINNAME _setting_2 = array(
"sid" => "NULL",
"name" => "PLUGIN SETTING NAME",
"title" => "PLUGIN TITLE",
"description" => "PLUGIN DESCRIPTION",
"optionscode" => "text",
"value" => PLUGIN VALUE",
"disporder" => "2",
"gid" => intval($gid),
);

$db->insert_query(TABLE_PREFIX."settings", $PLUGINNAME_setting_2);

}

// This function runs when the plugin is deactivated.
function pluginname_deactivate()
{

require '../inc/adminfunctions_templates.php';
global $db;

$db->query("DELETE FROM ".TABLE_PREFIX."settings WHERE name IN('$PLUGIN SETTING NAME', '$PLUGIN SETTING NAME')");Here you will replace all your PLUGIN SETTING NAMES into this array
$db->query("DELETE FROM ".TABLE_PREFIX."settinggroups WHERE name='GROUPNAME'"); The groupname should be the same as the one created above in the activation
}

// Functions
function FUNCTION NAME(){

}

?>

At this point you have an actual plugin sadly it doesn't do anything. That's next.


Possibly Related Threads...
Thread Author Replies Views Last Post
  [Tutorial]Add page to mybb Angel 13 8,458 Nov 24, 2017, 11:02 PM
Last Post: Mercuy
Question Any way to make new users default to multiple groups? Jayfore 1 967 Oct 11, 2017, 06:06 PM
Last Post: DrBaja
Exclamation bans.php Plugin? Salsut 14 8,942 Jul 03, 2017, 08:15 PM
Last Post: Farrahs
  Ficon, make it work with 1.8 Kiggoz 8 4,196 Mar 02, 2017, 05:49 AM
Last Post: sova174
  mytab plugin does not function on my theme bushido 1 1,501 Jul 04, 2016, 11:01 PM
Last Post: Prince Tony

Forum Jump:


Users browsing this thread: 1 Guest(s)