MyBB Community Forums

Full Version: Plugin Dev Guide
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Is there some plugin developer guide available? I would like develop a plugin, which adds 3 or more additional database tables and use that tables for some special user detail skills. Is that possible with myBB plugins? Is there some starting point for me?
And there you go.

https://docs.mybb.com/1.8/development/plugins/

That is the most usefull userguide to begin, then there are a lot of tuts available into this communitty from some developers Smile
Many thanks for the documentation. That helped for the beginning. At least I managed to add an additional link to the main navigation. This is only a dummy, which points into the vacuum.

But the goal is to click on this link and to execute new and not existing functions when clicking on the link. Data should be retrieved from the database and displayed. The respective template or at least header and footer of the forum, including the stylesheets, should be preserved. I assume this works via hooks. I have a list of hooks but how do I define hooks myself? Suppose I want to get values from a SELECT from the DB when the newly defined Action getSkills() is called. If that was successful, these values should be output in a table. I would have guessed now that the link defined above must look something like this:


<a href="index.php?action=sSkills">Skill</a>

I assume now in my plugin there should be a line like:

$plugins->add_hook('sSkills', 'showSkills');

and a function:


function showSkills() {
global $db;
// fire the select and pull the data from the table
}

But that seems not to work.

Or must the link read
<a href="myplugin.php?action=sSkills">Skill</a>

and i must use my single plugin.php file and do something like:

$action = $_GET['action'];


The "End User Display"/"View" does not need the forum tables, it defines his own structure. But i want to keep the overall look of the forum and use the registration.
THe plugin file uses $plugins->add_hook. Your main file should use $plugins->run_hooks. This guide I wrote may also be of assistance. I haven't touched permission viewer that it mentions in some time though so I'm not sure if that is up to date.
(2019-03-31, 12:30 PM)dragonexpert Wrote: [ -> ]THe plugin file uses $plugins->add_hook.  Your main file should use $plugins->run_hooks.  This guide I wrote may also be of assistance.  I haven't touched permission viewer that it mentions in some time though so I'm not sure if that is up to date.

Many thx. What do you mean with main file? Currently, i use only 1 php file with the name of the plugin: "skills.php". That's what i call "Plugin File". Currently, i t has only the standard things and at the start:


$plugins->run_hooks('sSkills');
$plugins->add_hook("sSkills", "showSkills");


and


function showSkills() {
    header("location: https://www.google.com");
}
Can you post your full code? You need to hook into the index (index_start should do) and then in your function you basically want to do something like:

if($mybb->get_input('action') == 'sSkills')
{

// Code to execute here

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

require_once MYBB_ROOT."/inc/adminfunctions_templates.php";

$plugins->add_hook("index_start","showSkills");

function plugin_info() {
$codename = str_replace('.php', '', basename(__FILE__));
return array(
// Set plugin vars
"guid" => "a94f1bb6-2045-4c0a-8dbb-6c4115a414f8",
"codename" => $codename,
"compatibility" => "18*"
);
}

function plugin_install() {
global $db;
   if(!session_id()) {
       @session_start();
   }

plugin_uninstall();

$collation = $db->build_create_table_collation();

if(!$db->table_exists('skills')) {
       $db->write_query("
           CREATE TABLE IF NOT EXISTS " . TABLE_PREFIX . "skills (
             `skillID` int(11) NOT NULL AUTO_INCREMENT,
             `skillName` varchar(255) NOT NULL,
             `skillLevel` smallint(3) NOT NULL DEFAULT '0',
             PRIMARY KEY (`skillID`),
 UNIQUE KEY `skillID` (`skillID`)
           ) ENGINE=MyISAM{$collation};");
   }
}

function plugin_is_installed() {
global $db;
$table_exists = false;

   $table_exists = $db->table_exists('skills');

return $table_exists;
}

function plugin_uninstall() {
global $db;
   if(!session_id()) {
       @session_start();
   }
if($db->table_exists('skills')) $db->drop_table('skills');

$db->delete_query('templategroups', "prefix='plugin'");
$db->delete_query('templates', "title='plugin' OR title LIKE 'plugin_%'");

}

function plugin_activate() {
$skillmenu = '<li><a href="?action=sSkills" class="skills">Skills</a></li>';
find_replace_templatesets('header', '#'.preg_quote('{$menu_search}').'#', $skillmenu."\n\t\t\t\t\t\t{\$menu_search}");
}

function plugin_deactivate() {
$skillmenu = '<li><a href="?action=sSkills" class="skills">Skills</a></li>';
find_replace_templatesets('header', '#\n\t\t\t\t\t\t'.$skillmenu.'#', '');
}

function showSkills() {
global $mybb;
if($mybb->get_input('action') == 'sSkills') {
header("location: https://www.google.com");
}
}
That's my current code. I have removed some vars here. It works only, to add the menu entry. But when I call ?action=sSkills, it doesn't load Google. The header() is only a placeholder. Instead of that, i will output later the values from the database.
See code below. There's a few things here I've corrected:
  • Apart from the info function, the rest of your functions should be prefixed with the plugin file name. For example, function showSkills_deactivate(). I've corrected this for you.
  • When running the uninstall() function within the install function, you haven't nested the $db->drop_table within the if statement so it won't work. I've corrected this for you.
  • Within the install() function, you've checked if the table skills exists before creating the table, which is fine, but you then go onto checking it again in the query. There's literally no need.

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

require_once MYBB_ROOT."/inc/adminfunctions_templates.php";

$plugins->add_hook("index_start","showSkills_action");

function showSkills_info() {
$codename = str_replace('.php', '', basename(__FILE__));
return array(
// Set plugin vars
"guid" => "a94f1bb6-2045-4c0a-8dbb-6c4115a414f8",
"codename" => $codename,
"compatibility" => "18*"
);
}

function showSkills_install() {
global $db;
   if(!session_id()) {
       @session_start();
   }

showSkills_uninstall();

$collation = $db->build_create_table_collation();

if(!$db->table_exists('skills')) {
       $db->write_query("
           CREATE TABLE " . TABLE_PREFIX . "skills (
             `skillID` int(11) NOT NULL AUTO_INCREMENT,
             `skillName` varchar(255) NOT NULL,
             `skillLevel` smallint(3) NOT NULL DEFAULT '0',
             PRIMARY KEY (`skillID`),
 UNIQUE KEY `skillID` (`skillID`)
           ) ENGINE=MyISAM{$collation};");
   }
}

function showSkills_is_installed() {
global $db;
$table_exists = false;

   $table_exists = $db->table_exists('skills');

return $table_exists;
}

function showSkills_uninstall() {
global $db;
   if(!session_id()) {
       @session_start();
   }
   
if($db->table_exists('skills'))
{
	$db->drop_table('skills');
}

$db->delete_query('templategroups', "prefix='plugin'");
$db->delete_query('templates', "title='plugin' OR title LIKE 'showSkills_%'");

}

function showSkills_activate() {
$skillmenu = '<li><a href="{$mybb->settings[\'bburl\']}/index.php?action=sSkills" class="skills">Skills</a></li>';
find_replace_templatesets('header', '#'.preg_quote('{$menu_search}').'#', $skillmenu."\n\t\t\t\t\t\t{\$menu_search}");
}

function showSkills_deactivate() {
$skillmenu = '<li><a href="{$mybb->settings[\'bburl\']}/index.php?action=sSkills" class="skills">Skills</a></li>';
find_replace_templatesets('header', '#\n\t\t\t\t\t\t'.$skillmenu.'#', '');
}

function showSkills_action() {
global $mybb;

if($mybb->get_input('action') == 'sSkills') 
{
	
header("location: https://www.google.com");

}

}
(2019-03-31, 09:35 PM)Wires Wrote: [ -> ]See code below. There's a few things here I've corrected:
  • Apart from the info function, the rest of your functions should be prefixed with the plugin file name. For example, function showSkills_deactivate(). I've corrected this for you.
  • When running the uninstall() function within the install function, you haven't nested the $db->drop_table within the if statement so it won't work. I've corrected this for you.
  • Within the install() function, you've checked if the table skills exists before creating the table, which is fine, but you then go onto checking it again in the query. There's literally no need.
Thank you for the adjustments and comments. I took them over and tested them but unfortunately it still doesn't work.

I had named the file for my plugin "plugin.php". Therefore all given functions were prefixed with "plugin_". In the *_uninstall() function the drop-table statement is inside the if condition. Since it is only one line, no braces are needed and you can write it as a single line. At least this has always worked for me in other PHP projects. Of course you are right with the *_install function. So I removed the if statement.

But if I now call my board with https://mydomain.com/mybb/?action=action=sSkills, it will still not be redirected to Google.
"Hello World!" plugin included with the package is the best guide.
Read it like a story book.
Pages: 1 2