MyBB Community Forums

Full Version: Plugin Authoring For Beginners [Part 1] / How to create plugins
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3 4 5 6
Sorry for the late reply

You need to insert the setting in a query each time, however you only need to rebuild the settings once, so you could use something like this:

$myfirstplugin_setting_1 = array(
        'sid'            => 'NULL',
        'name'        => 'myfirstplugin_enable',
        'title'            => 'Do you want to enable My First Plugin?',
        'description'    => 'If you set this option to yes, this plugin be active on your board.',
        'optionscode'    => 'yesno',
        'value'        => '1',
        'disporder'        => 1,
        'gid'            => intval($gid),
    );
$myfirstplugin_setting_2 = array(
        'sid'            => 'NULL',
        'name'        => 'myfirstplugin_something',
        'title'            => 'My First Plugin',
        'description'    => 'My First Plugin - description',
        'optionscode'    => 'yesno',
        'value'        => '1',
        'disporder'        => 2,
        'gid'            => intval($gid),
    );
$myfirstplugin_setting_3 = array(
        'sid'            => 'NULL',
        'name'        => 'myfirstplugin_somethingelse',
        'title'            => 'My First Plugin',
        'description'    => 'My First Plugin - description',
        'optionscode'    => 'yesno',
        'value'        => '1',
        'disporder'        => 3,
        'gid'            => intval($gid),
    );
$myfirstplugin_setting_4 = array(
        'sid'            => 'NULL',
        'name'        => 'myfirstplugin_anything',
        'title'            => 'My First Plugin',
        'description'    => 'My First Plugin - description',
        'optionscode'    => 'yesno',
        'value'        => '1',
        'disporder'        => 4,
        'gid'            => intval($gid),
    );

$db->insert_query('settings', $myfirstplugin_setting_1);
$db->insert_query('settings', $myfirstplugin_setting_2);
$db->insert_query('settings', $myfirstplugin_setting_3);
$db->insert_query('settings', $myfirstplugin_setting_4);
rebuild_settings();

As for question 2:

If one of my settings contained a text area, like below:
$myfirstplugin_setting_2 = array(
        'sid'            => 'NULL',
        'name'        => 'myfirstplugin_input',
        'title'            => 'My First Plugin',
        'description'    => 'My First Plugin - description',
        'optionscode'    => 'textarea',
        'value'        => '',
        'disporder'        => 2,
        'gid'            => intval($gid),
    );

You can echo the contents of that setting like this:
echo $mybb->settings['myfirstplugin_input'];

Notice myfirstplugin_input is the name of the setting.
I use Plugin Uploader and my plugin isn't, well, uploading.

<?php
if(!defined("IN_MYBB"))
{
    die("You Cannot Access This File Directly. Please Make Sure IN_MYBB Is Defined.");
} 
$plugins->add_hook('global_start', 'simplenewsbar_global_start'); 
function simplenewsbar_info()
{
return array(
        "name"  => "Simple News Bar",
        "description"=> "Create a short news bar that will be displayed at the top",
        "website"        => "http://seabtech.empirehostings.net/",
        "author"        => "Seabody",
        "authorsite"    => "http://seabtech.empirehostings.net/",
        "version"        => "1.0",
        "guid"             => "",
        "compatibility" => "16*"
    );
} 
function simplenewsbar_activate() {
global $db;

$simplenewsbar_group = array(
        'gid'    => 'NULL',
        'name'  => 'simplenewsbar',
        'title'      => 'Simple News Bar Settings',
        'description'    => 'Customise the settings for the Simple News Bar plugin',
        'disporder'    => "400",
        'isdefault'  => "0",
    );
$db->insert_query('settinggroups', $simplenewsbar_group);
 $gid = $db->insert_id(); 	
$simplenewsbar_setting_1 = array(
        'sid'            => 'NULL',
        'name'        => 'simplenewsbar_enable',
        'title'            => 'Power On?',
        'description'    => 'If you set this option to yes, this plugin will be active on your board.',
        'optionscode'    => 'yesno',
        'value'        => '1',
        'disporder'        => 1,
        'gid'            => intval($gid),
    ); 
	$simplenewsbar_setting_2 = array(
        'sid'            => 'NULL',
        'name'        => 'simplenewsbar_input',
        'title'            => 'Content',
        'description'    => 'What do you want the news bar to read? HTML is allowed.',
        'optionscode'    => 'textarea',
        'value'        => '1',
        'disporder'        => 2,
        'gid'            => intval($gid),
    ); 
	$db->insert_query('settings', $simplenewsbar_setting_1);
	$db->insert_query('settings', $simplenewsbar_setting_2);
	rebuild_settings(); 
	function simplenewsbar_deactivate()
  {
  global $db;
 $db->query("DELETE FROM ".TABLE_PREFIX."settings WHERE name IN ('simplenewsbar_enable')");
  $db->query("DELETE FROM ".TABLE_PREFIX."settings WHERE name IN ('simplenewsbar_input')");
    $db->query("DELETE FROM ".TABLE_PREFIX."settinggroups WHERE name='simplenewsbar'");
rebuild_settings();
 } 
 function simplenewsbar_global_start(){
global $mybb;

if ($mybb->settings['simplenewsbar_enable'] == 1){
 echo $mybb->settings['simplenewsbar_input'];
}
} 
 ?>

I also tried uploading it by FTP, and that caused my Plugins list to vanish.
If you indented your code properly you would see the syntax errors...
You forgot to end the simplenewsbar_activate() function. Before function simplenewsbar_deactivate() should be:

}

I've corrected your code below

<?php
if(!defined("IN_MYBB"))
{
    die("You Cannot Access This File Directly. Please Make Sure IN_MYBB Is Defined.");
} 
$plugins->add_hook('global_start', 'simplenewsbar_global_start'); 
function simplenewsbar_info()
{
return array(
        "name"  => "Simple News Bar",
        "description"=> "Create a short news bar that will be displayed at the top",
        "website"        => "http://seabtech.empirehostings.net/",
        "author"        => "Seabody",
        "authorsite"    => "http://seabtech.empirehostings.net/",
        "version"        => "1.0",
        "guid"             => "",
        "compatibility" => "16*"
    );
} 
function simplenewsbar_activate() {
global $db;

$simplenewsbar_group = array(
        'gid'    => 'NULL',
        'name'  => 'simplenewsbar',
        'title'      => 'Simple News Bar Settings',
        'description'    => 'Customise the settings for the Simple News Bar plugin',
        'disporder'    => "400",
        'isdefault'  => "0",
    );
$db->insert_query('settinggroups', $simplenewsbar_group);
 $gid = $db->insert_id();     
$simplenewsbar_setting_1 = array(
        'sid'            => 'NULL',
        'name'        => 'simplenewsbar_enable',
        'title'            => 'Power On?',
        'description'    => 'If you set this option to yes, this plugin will be active on your board.',
        'optionscode'    => 'yesno',
        'value'        => '1',
        'disporder'        => 1,
        'gid'            => intval($gid),
    ); 
    $simplenewsbar_setting_2 = array(
        'sid'            => 'NULL',
        'name'        => 'simplenewsbar_input',
        'title'            => 'Content',
        'description'    => 'What do you want the news bar to read? HTML is allowed.',
        'optionscode'    => 'textarea',
        'value'        => '1',
        'disporder'        => 2,
        'gid'            => intval($gid),
    ); 
    $db->insert_query('settings', $simplenewsbar_setting_1);
    $db->insert_query('settings', $simplenewsbar_setting_2);
    rebuild_settings(); 
}
    function simplenewsbar_deactivate()
  {
  global $db;
 $db->query("DELETE FROM ".TABLE_PREFIX."settings WHERE name IN ('simplenewsbar_enable')");
  $db->query("DELETE FROM ".TABLE_PREFIX."settings WHERE name IN ('simplenewsbar_input')");
    $db->query("DELETE FROM ".TABLE_PREFIX."settinggroups WHERE name='simplenewsbar'");
rebuild_settings();
 } 
 function simplenewsbar_global_start(){
global $mybb;

if ($mybb->settings['simplenewsbar_enable'] == 1){
 echo $mybb->settings['simplenewsbar_input'];
}
}
 ?>
Very good tutorial, well done OP!
(2012-09-20, 04:38 PM)HybridForum Wrote: [ -> ]Very good tutorial, well done OP!

Thank you Smile
(2012-09-20, 04:27 PM)Vernier Wrote: [ -> ]You forgot to end the simplenewsbar_activate() function. Before function simplenewsbar_deactivate() should be:

}

I've corrected your code below

<?php
if(!defined("IN_MYBB"))
{
    die("You Cannot Access This File Directly. Please Make Sure IN_MYBB Is Defined.");
} 
$plugins->add_hook('global_start', 'simplenewsbar_global_start'); 
function simplenewsbar_info()
{
return array(
        "name"  => "Simple News Bar",
        "description"=> "Create a short news bar that will be displayed at the top",
        "website"        => "http://seabtech.empirehostings.net/",
        "author"        => "Seabody",
        "authorsite"    => "http://seabtech.empirehostings.net/",
        "version"        => "1.0",
        "guid"             => "",
        "compatibility" => "16*"
    );
} 
function simplenewsbar_activate() {
global $db;

$simplenewsbar_group = array(
        'gid'    => 'NULL',
        'name'  => 'simplenewsbar',
        'title'      => 'Simple News Bar Settings',
        'description'    => 'Customise the settings for the Simple News Bar plugin',
        'disporder'    => "400",
        'isdefault'  => "0",
    );
$db->insert_query('settinggroups', $simplenewsbar_group);
 $gid = $db->insert_id();     
$simplenewsbar_setting_1 = array(
        'sid'            => 'NULL',
        'name'        => 'simplenewsbar_enable',
        'title'            => 'Power On?',
        'description'    => 'If you set this option to yes, this plugin will be active on your board.',
        'optionscode'    => 'yesno',
        'value'        => '1',
        'disporder'        => 1,
        'gid'            => intval($gid),
    ); 
    $simplenewsbar_setting_2 = array(
        'sid'            => 'NULL',
        'name'        => 'simplenewsbar_input',
        'title'            => 'Content',
        'description'    => 'What do you want the news bar to read? HTML is allowed.',
        'optionscode'    => 'textarea',
        'value'        => '1',
        'disporder'        => 2,
        'gid'            => intval($gid),
    ); 
    $db->insert_query('settings', $simplenewsbar_setting_1);
    $db->insert_query('settings', $simplenewsbar_setting_2);
    rebuild_settings(); 
}
    function simplenewsbar_deactivate()
  {
  global $db;
 $db->query("DELETE FROM ".TABLE_PREFIX."settings WHERE name IN ('simplenewsbar_enable')");
  $db->query("DELETE FROM ".TABLE_PREFIX."settings WHERE name IN ('simplenewsbar_input')");
    $db->query("DELETE FROM ".TABLE_PREFIX."settinggroups WHERE name='simplenewsbar'");
rebuild_settings();
 } 
 function simplenewsbar_global_start(){
global $mybb;

if ($mybb->settings['simplenewsbar_enable'] == 1){
 echo $mybb->settings['simplenewsbar_input'];
}
}
 ?>

I knew it would be something simple. Blush Thanks.

@frostschutz Bad habit, I know. I've never done it, so I didn't do it now. I should start though - thanks for that. Smile
(2012-09-21, 05:50 AM)Seabody Wrote: [ -> ]@frostschutz Bad habit, I know. I've never done it, so I didn't do it now. I should start though - thanks for that. Smile

A good editor will do most of the work for you actually. In PHP the indentation can be 99% automated.
I indented my code this time around. Again, an error uploading. I rewrote from scratch (no copy-paste). I narrowed it down to the activate function. This thing is driving me insane. I am 99% sure that there are no functions left open or anything like that.

	function simplenewsbar_activate()
		{
			global $db;
			
			$simplenewsbar_group = array(
				'gid' => 'NULL',
				'name' => 'simplenewsbar',
				'title' => 'Simple News Bar',
				'description' => 'Settings for the Simple News Bar plugin',
				'disporder' => "1",
				'isdefault' => "0",
			);
			$db->insert_query('settinggroups', $simplenewsbar_group);
			$gid = $db->insert_id();
			
			$simplenewsbar_setting = array(
				'sid' => 'NULL',
				'name' => 'simplenewsbar_input',
				'title' => 'Text to Display',
				'description' => 'Enter the text you want to display',
				'optionscode' => 'textarea',
				'value' => 'It lives',
				'disporder' => '1',
				'gid' => intval($gid),
			);
			$db-insert_query('settings', $simplenewsbar_setting);
			rebuild_settings();
		}

Note: I realize that the disporder for the simplenewsbar_setting array varies from the tutorial. That was me seeing if a lack of quotations were the problem.
There's nothing wrong with this function you posted, at least nothing that would prevent uploading the plugin
Pages: 1 2 3 4 5 6